在MVC / C#格式化字符串 [英] formatting string in MVC /C#

查看:171
本文介绍了在MVC / C#格式化字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串731478718861993983,我想用C#来得到这个73-1478-7188-6199-3983。我怎么能格式化这样?

I have a string 731478718861993983 and I want to get this 73-1478-7188-6199-3983 using C#. How can I format it like this ?

感谢。

推荐答案

如果你正在处理一个号码,你可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.globalization.numberformatinfo.aspx\"><$c$c>NumberFormatInfo对其进行格式化:

If you're dealing with a long number, you can use a NumberFormatInfo to format it:

首先,定义你的NumberFormatInfo(您可能需要额外的参数,这些都是基本3):

First, define your NumberFormatInfo (you may want additional parameters, these are the basic 3):

NumberFormatInfo format = new NumberFormatInfo();
format.NumberGroupSeparator = "-";
format.NumberGroupSizes = new[] { 4 };
format.NumberDecimalDigits = 0;        

接下来,你可以用它在你的号码:

Next, you can use it on your numbers:

long number = 731478718861993983;
string formatted = number.ToString("n", format);
Console.WriteLine(formatted);

毕竟,.NET具有非常不错的全球化支持 - !你更好的服务,使用它

After all, .Net has very good globalization support - you're better served using it!

这篇关于在MVC / C#格式化字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆