简体   繁体   English

使用 .ToString() 在 C# 中格式化十进制字符串

[英]Format Decimal String in C# using .ToString()

I have an amount stored in a decimal result :我有一个存储在十进制result的金额:

Eg.例如。 decimal result = 1935698.27;

How can I format that to a String using .ToString() so that it looks like this: R 1 935 698,27如何使用.ToString()将其格式化为String ,使其看起来像这样: R 1 935 698,27

It needs to do the following:它需要执行以下操作:

  • Add R (take note of the space) to the front of the StringR (注意空格)添加到String的前面
  • Add spaces to group the value in 3's (ie R 00 000 000 000,00 )添加空格以将值分组为 3(即R 00 000 000 000,00
  • Replace the .更换. with a ,,

This formats according to the South African Rand currency format.此格式根据南非兰特货币格式进行设置。

This is what I have:这就是我所拥有的:

decimal result = 1935698.27;
string strResult = result.ToString("What should I put here?"); // Need help

// Expected strResult = "R 1 935 698,27"

Any help is much appreciated!任何帮助深表感谢!

This is built in - just use the South African culture and the currency format specifier .这是内置的 - 仅使用南非文化和货币格式说明符

var result = 1935698.27m;
var ci = new CultureInfo("en-ZA");
var strResult = result.ToString("C", ci);
// strResult is R 1 935 698,27

See this fiddle .看到这个小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM