C# 双格式问题

发布于 2024-10-09 20:36:53 字数 189 浏览 5 评论 0原文

我正在使用下一个代码来格式化双精度值:

String.Format("${0:0,0.#}",...);

它工作得很好,但是当数字小于 10 时,我遇到了问题。例如,数字显示为 $03、$06。

请告诉我正确的字符串,使其具有下一个格式的双数 ddd,ddd,ddd,ddd.dd

I am using next code to format double value:

String.Format("${0:0,0.#}",...);

It working great, but when numbers are less than 10, I got problem. Numbers are displayed as $03, $06 for example.

Please advise me correct string to have a double number in next format ddd,ddd,ddd,ddd.dd

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-10-16 20:36:53

请尝试以下操作:

string result = string.Format("${0:#,##0.00}", d);

如果您的双精度代表您应该使用的货币:

string result = string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:c}", d);

请注意,如果您省略 CultureInfo.InvariantCulture,它可能会在某些计算机上使用 $ 以外的其他内容显示。例如,在我的计算机上 string.Format("{0:c}", d) 给出 2,00 kr 这可能不是您想要的。

在您的示例中,您实际上根本不需要使用 string.Format 。你可以用它来代替:

string s = d.ToString("c", CultureInfo.GetCultureInfo("en-US"));

除了更清晰、更简洁之外,它还有避免装箱的优点。当然,如果您的格式字符串比示例中的更复杂,那么使用 string.Format 是有意义的。

作为最后的评论,我建议不要使用双精度数来存储货币。小数类型可能更合适。

Try this instead:

string result = string.Format("${0:#,##0.00}", d);

If your double represents a currency you should use:

string result = string.Format(CultureInfo.GetCultureInfo("en-US"), "{0:c}", d);

Note that if you omit the CultureInfo.InvariantCulture it could display using something other than $ on some computers. For example on my computer string.Format("{0:c}", d) gives 2,00 kr which might not be what you wanted.

In your example you don't actually need to use string.Format at all. You could use this instead:

string s = d.ToString("c", CultureInfo.GetCultureInfo("en-US"));

As well as being clearer and more concise, it also has the advantage of avoiding boxing. Of course if your format string is more complex than in your example then it would make sense to use string.Format.

And as a final remark I'd recommend against using doubles to store currency. A decimal type is probably more appropriate.

冬天旳寂寞 2024-10-16 20:36:53

使用货币格式:

String.Format("{0:C}", money);

Use currency formatting:

String.Format("{0:C}", money);
书间行客 2024-10-16 20:36:53
String.Format("{0:C}", myDecimal);

或者
myDecimal.ToString("C");

将显示到小数点后两位,包括逗号分隔符并包括美元符号(基于区域性设置)。如果您希望它保留 1 位或 2 位以上小数,请在 C 后添加一个数字(即 C3)

String.Format("{0:C}", myDecimal);

or
myDecimal.ToString("C");

will display to two decimal places, include the comma separator and include the dollar sign (based on culture settings) in one fell swoop. If you want it to go to 1 or more than 2 decimal places, include a number after the C (i.e. C3)

迎风吟唱 2024-10-16 20:36:53
Digits after decimal point
// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

Thousands separator
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"

Zero
Following code shows how can be formatted a zero (of double type).

String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // ""

Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

Custom formatting for negative numbers and zero
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"

Some funny examples
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"
Digits after decimal point
// just two decimal places
String.Format("{0:0.00}", 123.4567);      // "123.46"
String.Format("{0:0.00}", 123.4);         // "123.40"
String.Format("{0:0.00}", 123.0);         // "123.00"

// max. two decimal places
String.Format("{0:0.##}", 123.4567);      // "123.46"
String.Format("{0:0.##}", 123.4);         // "123.4"
String.Format("{0:0.##}", 123.0);         // "123"
// at least two digits before decimal point
String.Format("{0:00.0}", 123.4567);      // "123.5"
String.Format("{0:00.0}", 23.4567);       // "23.5"
String.Format("{0:00.0}", 3.4567);        // "03.5"
String.Format("{0:00.0}", -3.4567);       // "-03.5"

Thousands separator
String.Format("{0:0,0.0}", 12345.67);     // "12,345.7"
String.Format("{0:0,0}", 12345.67);       // "12,346"

Zero
Following code shows how can be formatted a zero (of double type).

String.Format("{0:0.0}", 0.0);            // "0.0"
String.Format("{0:0.#}", 0.0);            // "0"
String.Format("{0:#.0}", 0.0);            // ".0"
String.Format("{0:#.#}", 0.0);            // ""

Align numbers with spaces
String.Format("{0,10:0.0}", 123.4567);    // "     123.5"
String.Format("{0,-10:0.0}", 123.4567);   // "123.5     "
String.Format("{0,10:0.0}", -123.4567);   // "    -123.5"
String.Format("{0,-10:0.0}", -123.4567);  // "-123.5    "

Custom formatting for negative numbers and zero
String.Format("{0:0.00;minus 0.00;zero}", 123.4567);   // "123.46"
String.Format("{0:0.00;minus 0.00;zero}", -123.4567);  // "minus 123.46"
String.Format("{0:0.00;minus 0.00;zero}", 0.0);        // "zero"

Some funny examples
String.Format("{0:my number is 0.0}", 12.3);   // "my number is 12.3"
String.Format("{0:0aaa.bbb0}", 12.3);          // "12aaa.bbb3"
深海蓝天 2024-10-16 20:36:53
String.Format("${0:#,0.#}",...); 

应该这样做。
请参阅自定义数字格式字符串

String.Format("${0:#,0.#}",...); 

should do it.
See Custom Numeric Format Strings

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文