格式十进制值字符串的前导空格 [英] Format decimal value to string with leading spaces
本文介绍了格式十进制值字符串的前导空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我如何格式化的十进制数为字符串使用逗号/小数点后一位数字和前导空格的值小于100?
例如, 12.3456
应该是输出的十进制值12.3
单前导空格。 10.011
是10.0
。 123.123
是123.1
我正在寻找一个解决方案,即兼容标准/自定义字符串格式化,即
十进制值= 12.345456;
Console.Write({0:魔术},值); //魔力将是一个奇特的图案。
解决方案
这种模式 {0,5:### 0}
应该工作:
的String.Format({0,5:### 0},12.3456)//输出12.3
的String.Format({0,5:### 0},10.011)//输出10.0
的String.Format({0,5:### 0},123.123)//输出123.1
的String.Format({0,5:### 0},1.123)//输出1.1
的String.Format({0,5:### 0},1234.123)//输出1234.1
How do I format a decimal value to a string with a single digit after the comma/dot and leading spaces for values less than 100?
For example, a decimal value of 12.3456
should be output as " 12.3"
with single leading space. 10.011
would be " 10.0"
. 123.123
is "123.1"
I'm looking for a solution, that works with standard/custom string formatting, i.e.
decimal value = 12.345456;
Console.Write("{0:magic}", value); // 'magic' would be a fancy pattern.
解决方案
This pattern {0,5:###.0}
should work:
string.Format("{0,5:###.0}", 12.3456) //Output " 12.3"
string.Format("{0,5:###.0}", 10.011) //Output " 10.0"
string.Format("{0,5:###.0}", 123.123) //Output "123.1"
string.Format("{0,5:###.0}", 1.123) //Output " 1.1"
string.Format("{0,5:###.0}", 1234.123)//Output "1234.1"
这篇关于格式十进制值字符串的前导空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文