开发者

.net standard format specifier to equivalent custom format

开发者 https://www.devze.com 2023-02-18 18:07 出处:网络
Is it possible to convert a standard number format specifier into it\'s equivalent custom format, e.g. n2 depending on the current culture might be #,##0.00.

Is it possible to convert a standard number format specifier into it's equivalent custom format, e.g. n2 depending on the current culture might be #,##0.00.

Basically is there a function that converts one to the other?

Thanks,

A开发者_开发知识库J


I can't find one line solution to format as #,##0.00. Adding an extra 0 after 3 digits causing the issue, so I did split this task in extra steps and format looks as #,###.00

int n2 = 123;

// adding extar '0' at the end
string result = String.Format("{0}", n2).PadRight(4, '0');

// convert result back to int.
n2 = Int32.Parse(result);

// Format: #,###.00
Console.WriteLine("Format: #,###.00 \t" + 
                   n2.ToString("0,0.00", CultureInfo.InvariantCulture));
0

精彩评论

暂无评论...
验证码 换一张
取 消