vb.net小数位数显示

小白白_ 2013-11-14 11:20:59
我在vb.net显示时想要只显示固定位数,在Matlab中对应的是format short g,也就是会从第一个非0数开始固定输出4位数,比如123.4345就会输出成123.4;0.0012323就会输出成0.001232。请问各位大神如何在vb.net里实现这个功能,要用什么函数么?
...全文
660 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
小白白_ 2013-11-15
  • 打赏
  • 举报
回复
引用 16 楼 wind_cloud2011 的回复:
比较笨的方法,有空再想想好的方法,
 string S;
        string 取整;
        public string getsingle(double t) 
        { 
            if (t > 1) 
            {
                if (t.ToString().Length > 4)
                {
                    S = (t.ToString()).Substring(0, 4);
                    if (S.IndexOf(".") > 0)
                    {
                        S = (t.ToString()).Substring(0, 5);
                    }
                }
                else
                {
                    S = t.ToString();
                }

            } 
            else if (t < 1) 
            { 
                string w = t.ToString(); 
                string[] split = null; 
                split = w.Split('.'); 
                int 小数部分 = ((int)(Math.Pow(10, split[1].Length) * t)); 
                //string 取整 = (小数部分.ToString()).Substring(0, 4);
                if (小数部分.ToString().Length <= 4)
                { 
                    取整 = 小数部分.ToString();
                    S = (Convert.ToInt16(取整) / Math.Pow(10, split[1].Length)).ToString(); 
                }
                else
                { 
                    取整 = (小数部分.ToString()).Substring(0, 4);
                    S = (Convert.ToInt16(取整) / Math.Pow(10, 4)).ToString(); 
                }   
                
            } 
            return S; 
        }
嗯,好的谢了,分数给你吧
无涯大者 2013-11-14
  • 打赏
  • 举报
回复
试试 vb.net 里面的ToString函数;  例如 25.123.ToString("F2"); // 25.12 "F?"表示保持几位小数
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
比较笨的方法,有空再想想好的方法,
 string S;
        string 取整;
        public string getsingle(double t) 
        { 
            if (t > 1) 
            {
                if (t.ToString().Length > 4)
                {
                    S = (t.ToString()).Substring(0, 4);
                    if (S.IndexOf(".") > 0)
                    {
                        S = (t.ToString()).Substring(0, 5);
                    }
                }
                else
                {
                    S = t.ToString();
                }

            } 
            else if (t < 1) 
            { 
                string w = t.ToString(); 
                string[] split = null; 
                split = w.Split('.'); 
                int 小数部分 = ((int)(Math.Pow(10, split[1].Length) * t)); 
                //string 取整 = (小数部分.ToString()).Substring(0, 4);
                if (小数部分.ToString().Length <= 4)
                { 
                    取整 = 小数部分.ToString();
                    S = (Convert.ToInt16(取整) / Math.Pow(10, split[1].Length)).ToString(); 
                }
                else
                { 
                    取整 = (小数部分.ToString()).Substring(0, 4);
                    S = (Convert.ToInt16(取整) / Math.Pow(10, 4)).ToString(); 
                }   
                
            } 
            return S; 
        }
小白白_ 2013-11-14
  • 打赏
  • 举报
回复
引用 14 楼 wind_cloud2011 的回复:
明天再帮你修改,我知道原因了
恩,好,谢谢啦,我比较菜,vb.net刚用
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
明天再帮你修改,我知道原因了
小白白_ 2013-11-14
  • 打赏
  • 举报
回复
引用 12 楼 wind_cloud2011 的回复:
小数部分的:
 string w = t.ToString();
             string[] split = null;
             split = w.Split('.');
             int 小数部分= ((int)(Math.Pow(10, split[1].Length) * t));

             if (小数部分.ToString().Length <= 4)
             {
                  取整 = 小数部分.ToString();
             }
             else
             {
                取整 = (小数部分.ToString()).Substring(0, 4);

             }

             S = (Convert.ToInt16(取整)/ Math.Pow(10, split[1].Length)).ToString();
不好意思再麻烦你一下,还是有点小问题,就是我输入0.123456时,返回的0.001234,后来我发现当我输入的有效数字比4多了x个时,就会在返回的值前多加x个0,即如果我输入0.12345,这个比4个位数多1,那么输出的就成了0.01234,这个是哪句话有问题呢,谢了
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
小数部分的:
 string w = t.ToString();
             string[] split = null;
             split = w.Split('.');
             int 小数部分= ((int)(Math.Pow(10, split[1].Length) * t));

             if (小数部分.ToString().Length <= 4)
             {
                  取整 = 小数部分.ToString();
             }
             else
             {
                取整 = (小数部分.ToString()).Substring(0, 4);

             }

             S = (Convert.ToInt16(取整)/ Math.Pow(10, split[1].Length)).ToString();
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
我只考虑整数为4位以上了,要修改一下
小白白_ 2013-11-14
  • 打赏
  • 举报
回复
引用 9 楼 wind_cloud2011 的回复:
 string S;
     public string getsingle(double t)
     {
         if (t > 1)
         {
             S = (t.ToString()).Substring(0, 4);
             if (S.IndexOf(".") > 0)
             {
                 S = (t.ToString()).Substring(0, 5);
             }            

         }
         else if (t<1)
         {
             string w = t.ToString();
             string[] split = null;
             split = w.Split('.');
             int 小数部分= ((int)(Math.Pow(10, split[1].Length) * t));
             string 取整=(小数部分.ToString()).Substring(0,4);
             S = (Convert.ToInt16(取整)/ Math.Pow(10, split[1].Length)).ToString();
         }
         return S;
     }
多谢你的程序。我试了一下这个程序,然后我输入0.0123的时候结果变成的0.00123,是不是哪里写错了啊
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
 string S;
     public string getsingle(double t)
     {
         if (t > 1)
         {
             S = (t.ToString()).Substring(0, 4);
             if (S.IndexOf(".") > 0)
             {
                 S = (t.ToString()).Substring(0, 5);
             }            

         }
         else if (t<1)
         {
             string w = t.ToString();
             string[] split = null;
             split = w.Split('.');
             int 小数部分= ((int)(Math.Pow(10, split[1].Length) * t));
             string 取整=(小数部分.ToString()).Substring(0,4);
             S = (Convert.ToInt16(取整)/ Math.Pow(10, split[1].Length)).ToString();
         }
         return S;
     }
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
大于1的,也要考虑1小点,如果取左4位,如果含有小数点的,再取5位,
小白白_ 2013-11-14
  • 打赏
  • 举报
回复
引用 4 楼 wind_cloud2011 的回复:
大于1的直接取左边4位,不够补上什么 小于1的要写:    取出小数部分,得到小数的位数,如0.0012323,小数位数是7,这个数*10的7次方,得到整数12323,取4位,再除10的7次, =========这样对吧?==========
是这个意思,但是我的输出数据比较多,我之前就是直接保留了4位小数,然后就是math.round(输出内容,4),保留有效数字有这种函数么
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
给你写个小于1的笨办法,应该有更好的:
   string w = t.ToString();
             string[] split = null;
             split = w.Split('.');
             int 小数部分= ((int)(Math.Pow(10, split[1].Length) * t));
             string 取整=(小数部分.ToString()).Substring(0,4);
             S = (Convert.ToInt16(取整)/ Math.Pow(10, split[1].Length)).ToString();
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
大于1的保留4位
wind_cloud2011 2013-11-14
  • 打赏
  • 举报
回复
大于1的直接取左边4位,不够补上什么 小于1的要写:    取出小数部分,得到小数的位数,如0.0012323,小数位数是7,这个数*10的7次方,得到整数12323,取4位,再除10的7次, =========这样对吧?==========
小白白_ 2013-11-14
  • 打赏
  • 举报
回复
引用 1 楼 fengqingtao2008 的回复:
试试 vb.net 里面的ToString函数;  例如 25.123.ToString("F2"); // 25.12 "F?"表示保持几位小数
这个我试了,但是只能保留固定的位数,不能保留固定的有效数字啊
水猿兵团五哥 2013-11-14
  • 打赏
  • 举报
回复
自己写个函数,从非0位开始留几位

16,555

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧