zoukankan      html  css  js  c++  java
  • C#浮点数保留位(转)

    C#浮点数保留位数

    这里用String.Forma("{0:F}",x);来解决。

    下面是试验和截图


    using System;  
    using System.Collections.Generic;
    using System.Linq;  
    using System.Text;  
    using System.Threading.Tasks;  
    namespace tst
    {
        class Program
        {
            static void Main(string[] args)
            {
                double b = 123456.123456789;
                Console.WriteLine(String.Format("{0:F}", b));//保留2位小数(默认)
                Console.WriteLine(String.Format("{0:F1}", b));//保留1位小数
                Console.WriteLine(String.Format("{0:F2}", b));//保留2位小数
                Console.WriteLine(String.Format("{0:F3}", b));//保留3位小数
                Console.WriteLine(String.Format("{0:F4}", b));//保留4位小数
                Console.WriteLine(String.Format("{0:F5}", b));//保留5位小数
                Console.WriteLine(String.Format("{0:F6}", b));//保留6位小数
                Console.WriteLine(String.Format("{0:F7}", b));//保留7位小数
                Console.WriteLine(String.Format("{0:F8}", b));//保留8位小数
                Console.WriteLine(String.Format("{0:F9}", b));//保留9位小数
                Console.WriteLine(String.Format("{0:F10}", b));//保留10位小数
                /*保留指定位小数*/
                int n = 5;
                Console.WriteLine("保留指定的位数:n = {0}",n);
                Console.WriteLine(String.Format("{0:F" + String.Format("{0}",n)+"}", b));//
                Console.ReadKey();
            }
        }
    }
    

    https://www.cnblogs.com/wudb527/p/9716457.html

  • 相关阅读:
    P3501 [POI2010]ANT-Antisymmetry
    P3498 [POI2010]KOR-Beads(hash表)
    UVA10298 Power Strings
    UVA1714 Keyboarding(bfs)
    P4289 [HAOI2008]移动玩具(bfs)
    Ubuntu分辨率太小的解决方案
    Ubuntu分辨率太小的解决方案
    主板亮红灯,显示器没信号
    主板亮红灯,显示器没信号
    VS注释与取消注释快捷键
  • 原文地址:https://www.cnblogs.com/xihong2014/p/15559485.html
Copyright © 2011-2022 走看看