1

I have an amount stored in a decimal 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

It needs to do the following:

  • Add R (take note of the space) to the front of the String
  • Add spaces to group the value in 3's (i.e 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!

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

This doesn't add a space between R and the amount.
It does - see the fiddle. I copy / pasted the output into the answer.
@BarryMichaelDoyle if you look at the docs for the currency specifier, you can see how you could create a custom NumberFormatInfo to create whatever format you liked. It helps that the framework has a default for each culture, so usually this isn't needed.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.