String.Format an integer to use a thousands separator with decimal value in danish culture

I have a string totalPRice which holds a value like this 1147,5
I want two things.
1)round the value so that there is always two digits after ,
2)Implement thousands separator in this string, So that final out put will be some thing like this 1.147,50
I have tried some thing like this

String.Format("{0:0.00}", totalPRice)

It does my first requirement correctly by producing an output 1147,50.
But I am way behind in my second requirement. Can any one tell me how I can achieve this?

Note: In danish culture . stands for , and , stands for .

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

You can refer to Standard Numeric Format Strings and use

string.Format("{0:N2}", 1234.56)

You may also specify the culture manually, if danish is not your default culture:

var danishCulture = CultureInfo.CreateSpecificCulture("da-DK");
string.Format(danishCulture, "{0:N2}", 1234.56);

see MSDN Reference for CultureInfo

Method 2

You should create a culture-specific CultureInfo object and use it when converting the number into a string. Also, you can set the default culture for your whole program.

Then, your code will look like this:

// create Dennmark-specific culture settings
CultureInfo danishCulture = new CultureInfo("da");

// format the number so that correct Danish decimal and group separators are used
decimal totalPrice = 1234.5m;
Console.WriteLine(totalPrice.ToString("#,###.##", danishCulture));

Note that . and , in the formatting string are specified opposit as you want. This is because they identify decimal and group separators, and are replaced with the correct culture specific-ones.

Method 3

Try this:

String.Format("{0:N2}", totalPRice)

Another possibility is to use the ToString(string format) overload.

totalPRice.ToString("N2");

Method 4

If this is a currency value (money!), then it’s better to use the current format specifier ‘C’ or ‘c’:

string.Format("{0:C}", 1234.56)

Normally I don’t write the number of decimal digits since it comes from the international configuration.
You may way to use a different colture specifier if you don’t want to use the default one.

var colture = CultureInfo.CreateSpecificCulture("§§§§§");
string.Format(culture, "{0:C}", 1234.56);

where §§§§§ is the string that identifies the desired colture.

Method 5

Try this for Price format. Put it under template field instead of BoundField.

<%#(decimal.Parse(Eval("YourDataField").ToString())).ToString("N2")%>


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x