双字符串格式问题 [英] double string format problem
问题描述
我的问题是。我试着以一种格式写数字。在此格式中,如果此数字是仅在数字后面放置的整数。
输入输出
12.012。
12.212.2
我试过
Hi, my problem is that. I tried to write number in a format. In this format, if this number is an integer only put after the digits.
Input Output
12.0 "12."
12.2 "12.2"
I tried
String.Format("{0:0.###}", number))
,但它不起作用。它将整数转换为不带点符号的字符串。
你能帮帮我吗?
谢谢你提前。
, but it is not work. It convert integer number to string without dot notation.
Can you help me pls?
Thanks in advance.
推荐答案
阅读有关String.Format和自定义数字格式的文档。
替换 #带有0的字符。
Read the documentation on String.Format and Custom Number Formats.
Replace the "#" characters with "0".
我不知道在没有任何小数位的数字之后显示小数点的任何文化设置(我已经通过这里安装的整个 CultureInfo.GetCultures
。你能得到的最接近的是Dave Kreskoviak的解决方案
I''m not aware of any culture settings that display a decimal point after a number that does not have any decimal places (I''ve been through the entire CultureInfo.GetCultures
that are installed here). The closest you can get is with Dave Kreskoviak''s solution of
String.Format("{0:0.0##}", number))
但是在给出上面的例子之后,这似乎并不是你想要的。
你必须覆盖正常的格式化过程,例如
but that doesn''t seem to be what you''re after given your example above.
You will have to override the normal formatting process e.g.
private string myDoubleToString(double d)
{
if (d % 1 == 0)
return d.ToString() + CultureInfo.CurrentCulture.NumberFormat.NumberDecimalSeparator;
else
return d.ToString();
}
或重新考虑为什么你想要在这些情况下得到小数点尾 - 这很奇怪。
or reconsider why you want the trailing decimal point in these circumstances - it''s rather strange.
这篇关于双字符串格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!