2011-12-07 59 views
2

可能重复:
Double.ToString with N Number of Decimal PlacesC# - 显示小数到6位小数

我想说明一个十进制到6位小数,即使它包含6×0的 对于例如:

3.000000 
5.100000 
3.456789 

等等,这是可能的吗?

+2

检查http://www.csharp-examples.net/string-format-double/ - 我敢肯定,这已被问了很多次,虽然 – ChrisF

+1

前一:http://stackoverflow.com/questions/3359916/format-to-two-decimal-places。两个:http://stackoverflow.com/questions/4884749/format-a-double-to-8-decimal-places。三:http://stackoverflow.com/questions/5168592/force-a-string-to-2-decimal-places。 –

回答

15

使用N6作为numeric format string

myDecimal.ToString("N6"); 

string.Format("{0:N6}", myDecimal); 
+0

问题在于,它将其转换为字符串,我需要将它保存为十进制格式,因为这是存储在数据库中的数据类型。 – CallumVass

+1

@BiffBaffBoff你想要的东西看起来很奇怪,因为十进制格式与格式化输出无关。 –

4
Decimal d = 20; 
d.ToString("0.000000");