如何在c#中将1700格式化为1'700和1000000格式化为1'000'000?

发布于 2024-08-18 21:09:15 字数 86 浏览 7 评论 0原文

我喜欢像数学一样格式化所有数字。是否有预定义的函数或者只能通过子字符串和替换来实现?

编辑:我的文化是 de-ch

最好的问候

I like to format all numbers like in math. is there a predefined function or is that just possible with substring and replace?

edit: my culture is de-ch

Best regards

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

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

发布评论

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

评论(5

呆萌少年 2024-08-25 21:09:15

试试这个

int input = Convert.ToInt32("1700");
string result = String.Format("{0:##,##}", input);

或者这个

Console.WriteLine(1700.ToString("##,##", new NumberFormatInfo() { NumberGroupSeparator = "'" })); 

Try this

int input = Convert.ToInt32("1700");
string result = String.Format("{0:##,##}", input);

Or this

Console.WriteLine(1700.ToString("##,##", new NumberFormatInfo() { NumberGroupSeparator = "'" })); 
夏の忆 2024-08-25 21:09:15
var numformat = new NumberFormatInfo {
                   NumberGroupSeparator = "'",
                   NumberGroupSizes = new int[] { 3 },
                   NumberDecimalSeparator = "."
                };
Console.WriteLine(1000000.ToString("N",numformat));
var numformat = new NumberFormatInfo {
                   NumberGroupSeparator = "'",
                   NumberGroupSizes = new int[] { 3 },
                   NumberDecimalSeparator = "."
                };
Console.WriteLine(1000000.ToString("N",numformat));
叹倦 2024-08-25 21:09:15

试试这个:

Console.WriteLine(1000000.ToString("#,##0").Replace(
    CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator, "'"));

或者

NumberFormatInfo likeInMath = new NumberFormatInfo()
{
    NumberGroupSeparator = "'"
};
Console.WriteLine(1000000.ToString("#,##0", likeInMath));

Try this:

Console.WriteLine(1000000.ToString("#,##0").Replace(
    CultureInfo.CurrentCulture.NumberFormat.NumberGroupSeparator, "'"));

Or

NumberFormatInfo likeInMath = new NumberFormatInfo()
{
    NumberGroupSeparator = "'"
};
Console.WriteLine(1000000.ToString("#,##0", likeInMath));
冷︶言冷语的世界 2024-08-25 21:09:15

使用 int.ToString() 和 < a href="http://msdn.microsoft.com/en-us/library/1ksz8yb7.aspx" rel="nofollow noreferrer">iFormatProvider。

另请参阅此处 msdn。

use int.ToString() and an iFormatProvider.

also take a look here msdn.

苏辞 2024-08-25 21:09:15

我总是使用这种格式

 "#,##0;#,##0'-';0"

,所以你可以使用它

 int input = Convert.ToInt32("100000000");  
 string result = String.Format("{#,##0;#,##0'-';0}", input);

I always Use this format

 "#,##0;#,##0'-';0"

so You can use it in

 int input = Convert.ToInt32("100000000");  
 string result = String.Format("{#,##0;#,##0'-';0}", input);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文