format number in C# [duplicate] - c#

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
How to format a number 1234567 into 1,234,567 in C#?

For format options for Int32.ToString(), see standard format strings or custom format strings.
For example:
string s = myIntValue.ToString("#,##0");
The same format options can be use in a String.Format, as in
string s = String.Format("the number {0:#,##0}!", myIntValue);
Do note that the , in that format doesn't specify a "use a comma" but rather that the grouping character for the current culture should be used, in the culture-specific positions.
You also do not need to specify a comma for every position. The fact that there is a comma in the format string means that the culture-specific grouping is used.
So you get "1 234 567 890" for pl-PL or "1,23,45,67,890" for hi-IN.

var decimalValue = 1234567m;
var value = String.Format("{0:N}", decimalValue); // 1,234,567.00
or without cents
var value = String.Format("{0:N0}", decimalValue); // 1,234,567

Try
String.Format("{0:##,####,####}", 8958712551)
For Examples have a look at http://www.csharp-examples.net/string-format-double/

Using your current locale's thousands separator:
int n = 1234567 ;
n.ToString("N0");
Or, use the overload to ToString, which takes the culture as a parameter.

string formatted = string.Format("{0:##,#}", 123456789);
It depends on the culture of your computer. Some countries use commas, some countries use dots. On my computer the output was: 123.456.789

Related

C# Currency Formatting

Is there any way to format a number in the currency format (in the current Culture) using a custom format string?
For example:
1525.00 -> $1,525 (no trailing zeros)
1525.25 -> $1,525.25 (show decimals only when necessary)
We have tried different formats but none of them can produce the above.
As far as I know, no, you can't do that with a "one" format since you don't want show decimal parts for the first one but you "want" to show decimals part for the second one. I don't think there will be a "simple" format for both.
You can check the Currency format specifier (C) for that as;
var v = 1525.00;
$"{v:C0}".Dump();
returns $1,525 and
var v = 1525.25;
$"{v:C2}".Dump();
return $1,525.25.
Just a note, string interpolation uses CurrentCulture settings and in that case, I assume your current culture is somewhat based on english-based or InvariantCulture for generate CurrencySymbol, NumberGroupSeparator and NumberDecimalSeparator as $, , and . respectively.
Does this count?
static string MyCustomCurrencyString(decimal d) => d % 1 == 0 ? $"{d:C0}" : $"{d:C2}";
Let's test it:
Console.WriteLine(ZerosAreBad(1525));
Console.WriteLine(ZerosAreBad(1525.25m));
It works!
$1,525
$1,525.25

C# - How do i get a slash in DATETIME, with System.Globalization.CultureInfo [duplicate]

This question already has answers here:
C# date formatting is losing slash separators
(3 answers)
Closed 3 years ago.
I want a date format in danish in this format
"Tor. 27/6"
Tor is short for torsdag, meaning thursday in danish
I have this code
string formatted = datetime.ToString("ddd. dd/MM", new System.Globalization.CultureInfo("da-DK"));
It's returning
"To. 27-6"
so it's using a dash and ignoring the slash.
How do i force it to use slash while still using cultureinfo danish?
Yes, the / character has the specific meaning of "culture specific date separator" in custom date/time format strings.
If you want a literal /, you need to quote it in the pattern:
var culture = new CultureInfo("da-DK");
string formatted = datetime.ToString("ddd. dd'/'MM", culture);
Output on my machine:
to. 27/06
It's using "06" instead of "6" because you've used MM in your format string - if you don't want zero padding for the day and month numbers, use "ddd. d'/'M" as the format string. That doesn't help the "to" become "Tor", admittedly. If you want that, you'd need to modify the culture's abbreviated day-of-week values.
surround the desired date seperator ('/' in this case) with single quote.
string formatted = datetime.ToString("ddd. dd'/'MM", new System.Globalization.CultureInfo("da-DK"));

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 .
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
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.
Try this:
String.Format("{0:N2}", totalPRice)
Another possibility is to use the ToString(string format) overload.
totalPRice.ToString("N2");
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.
Try this for Price format. Put it under template field instead of BoundField.
<%#(decimal.Parse(Eval("YourDataField").ToString())).ToString("N2")%>

Math.Round, keep decimal place [duplicate]

This question already has answers here:
Formatting a float to 2 decimal places
(9 answers)
Closed 9 years ago.
For example.
Math.Round(2.314, 2) //2.31
Math.Round(2.301, 2) //2.3 , but I want this as 2.30
Numbers don't have any conception of zeroes after a decimal point.
You're actually asking how to convert the number into a string with extra zeroes:
(2.301).ToString("0.00") // "2.30"
See numeric format strings for more detail.
In particular, the 0 specifier will round away from zero.
You want a string formatting of the number:
string val = Math.Round(2.301, 2).ToString("F2");
here's a post on formatting numbers in C#
2.3 and 2.30 are the same thing. If you want the string 2.30 then use .ToString("F2") on the Math.Round function.
2.3 and 2.30 is the same thing from a code perspective. You can display the trailing zero by formatting a string:
string yourString = Math.Round(2.301, 3).ToString("0.00");
The decimal is still there, you're probably just not seeing because when you look at the string representation, by default it will omit trailing zeros. You can overwrite this behavior by passing a format string to ToString():
Console.WriteLine(Math.Round(2.301, 2).ToString("N2")) // 2.30
But of course, if this is just for display purposes, you don't really need to call Math.Round:
Console.WriteLine(2.301.ToString("N2")) // 2.30
Further Reading
Standard Numeric Format Strings
Custom Numeric Format Strings
If you use decimal numbers (their literals end with m, for "money"), you get the behavior you're after. double numbers don't have a concept of significant zeroes the same way that decimals do.
Math.Round(2.314m, 2);
Math.Round(2.301m, 2);
Or if you want to change how you see the numbers, you can use a string format:
Math.Round(2.314, 2).ToString("N2");
Math.Round(2.301, 2).ToString("N2");

C# Numeric formatting question

I have these numbers:
5.25
10251.35
5
and I want them to be formatted with groups and always with 2 decimals ,XX
this is what .ToString("N2") does:
5,25
10.251,35
5
How can i make the '5' look like 5,00 too ?
And for multiple cultures of course (en: 5.0, de: 5,0 ...)
In fact this question has no sense, N2 should do it at all costs. The problem was in my WebServer.
Isn't this working for you:
decimal d = 5m;
string formatted = d.ToString("N2");
This will use the current culture, but you could specify one:
string formatted = d.ToString("N2", new CultureInfo("fr-FR"));
.ToString("{0:#,0.00}")
Note: although this uses US grouping & decimal symbols, the result will be locale aware, assuming that either your current user locale is set correctly or you pass a locale into ToString().
IIRC:
toString("0:0.00");
MSDN
With the format you have (Fixed-point), you should use:
.ToString("F2");
You have all the format here: MSDN

Categories