的String.Format - 如何格式化为x位数(不考虑小数位)? [英] String.Format - How can I format to x digits (regardless of decimal place)?

查看:720
本文介绍了的String.Format - 如何格式化为x位数(不考虑小数位)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要(在我的案件6包括小数点)格式化浮点数为x字符。我的输出还需要包括数字的符号



所以,给出的投入,这里有预期产出



<预类=郎无prettyprint-覆盖> 1.23456 => 1.2345

-12.34567 => -12.345

-0.123456 => -0.1234

1234.567 => 1234.5

请假设总有最后一个字符前的小数位。即不会有 12345.6 号码输入 - 输入将始终小于或等于 9999.9



我想这必须有条件地进行。


解决方案

下面是一个透明的办法做到这一点无需格式字符串(除了F):

 静态无效的主要()
{
双击Y = 1.23456;
Console.WriteLine(FormatNumDigits(Y,5));
Y = -12.34567;
Console.WriteLine(FormatNumDigits(Y,5));
Y = -0.123456;
Console.WriteLine(FormatNumDigits(Y,5));
Y = 1234.567;
Console.WriteLine(FormatNumDigits(Y,5));

Y = 0.00000234;
Console.WriteLine(FormatNumDigits(Y,5));

Y = 1.1;
Console.WriteLine(FormatNumDigits(Y,5));
}


公共字符串FormatNumDigits(双号,INT X){
串asString =(数字> = 0+:?)+ number.ToString(F50,System.Globalization.CultureInfo.InvariantCulture);

如果(asString.Contains()'。'){
如果(asString.Length> X + 2){
返回asString.Substring(0,X + 2 );
}其他{
//垫用零
返回asString.Insert(asString.Length,新的String(0,X + 2 - asString.Length));
}
}其他{
如果(asString.Length> X + 1){
返回asString.Substring(0,X + 1);
}其他{
//垫用零
返回asString.Insert(1,新的字符串('0',X + 1 - asString.Length));
}
}
}



输出:



  1.2345 
-12.345
-0.1234
1234.5
+ 0.0000
1.1000

修改



注意,它不砍掉尾随零。


I need to format a floating point number to x characters (6 in my case including the decimal point). My output also needs to include the sign of the number

So given the inputs, here are the expected outputs

1.23456   => +1.2345

-12.34567 => -12.345

-0.123456 => -0.1234

1234.567  => +1234.5

Please assume there is always a decimal place before the last character. I.e. there will be no 12345.6 number input - the input will always be less than or equal to 9999.9.

I'm thinking this has to be done conditionally.

解决方案

Here's a transparent way to do it without format strings (except for "F"):

  static void Main()
  {
     double y = 1.23456;
     Console.WriteLine(FormatNumDigits(y,5));
     y= -12.34567;
     Console.WriteLine(FormatNumDigits(y,5));
     y = -0.123456;
     Console.WriteLine(FormatNumDigits(y,5));
     y = 1234.567;
     Console.WriteLine(FormatNumDigits(y,5));

     y = 0.00000234;
     Console.WriteLine(FormatNumDigits(y,5));

     y = 1.1;
     Console.WriteLine(FormatNumDigits(y,5));
  }


  public string FormatNumDigits(double number, int x) {
     string asString = (number >= 0? "+":"") + number.ToString("F50",System.Globalization.CultureInfo.InvariantCulture);

     if (asString.Contains('.')) {
        if (asString.Length > x + 2) {
           return asString.Substring(0, x + 2);
        } else {
           // Pad with zeros
           return asString.Insert(asString.Length, new String('0', x + 2 - asString.Length));
        }
     } else {
        if (asString.Length > x + 1) {
           return asString.Substring(0, x + 1);
        } else {
           // Pad with zeros
           return asString.Insert(1, new String('0', x + 1 - asString.Length));
        }
     }
  }

Output:

  +1.2345
  -12.345
  -0.1234
  +1234.5
  +0.0000
  +1.1000

EDIT

Notice that it does not chop off trailing zeros.

这篇关于的String.Format - 如何格式化为x位数(不考虑小数位)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆