Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using String Format to show decimal up to 2 places or simple integer

I have got a price field to display which sometimes can be either 100 or 100.99 or 100.9, What I want is to display the price in 2 decimal places only if the decimals are entered for that price , for instance if its 100 so it should only show 100 not 100.00 and if the price is 100.2 it should display 100.20 similarly for 100.22 should be same . I googled and came across some examples but they didn't match exactly what i wanted :

// just two decimal places String.Format("{0:0.00}", 123.4567);      // "123.46" String.Format("{0:0.00}", 123.4);         // "123.40" String.Format("{0:0.00}", 123.0);         // "123.00" 
like image 703
Mr A Avatar asked Aug 05 '11 04:08

Mr A


People also ask

How do I format a string to two decimal places in Python?

2️⃣ f-string An f-string is a string literal in Python that has the prefix ' f ' containing expressions inside curly braces. These expressions can be replaced with their values. Thus, you can use f'{value: . 2f}' to return a string representation of the number up to two decimal places.

How do you turn a string into a decimal?

Converting a string to a decimal value or decimal equivalent can be done using the Decimal. TryParse() method. It converts the string representation of a number to its decimal equivalent.

How do you print numbers to two decimal places?

we now see that the format specifier "%. 2f" tells the printf method to print a floating point value (the double, x, in this case) with 2 decimal places. Similarly, had we used "%. 3f", x would have been printed rounded to 3 decimal places.


2 Answers

Sorry for reactivating this question, but I didn't find the right answer here.

In formatting numbers you can use 0 as a mandatory place and # as an optional place.

So:

// just two decimal places String.Format("{0:0.##}", 123.4567);      // "123.46" String.Format("{0:0.##}", 123.4);         // "123.4" String.Format("{0:0.##}", 123.0);         // "123" 

You can also combine 0 with #.

String.Format("{0:0.0#}", 123.4567)       // "123.46" String.Format("{0:0.0#}", 123.4)          // "123.4" String.Format("{0:0.0#}", 123.0)          // "123.0" 

For this formating method is always used CurrentCulture. For some Cultures . will be changed to ,.

Answer to original question:

The simpliest solution comes from @Andrew (here). So I personally would use something like this:

var number = 123.46; String.Format(number % 1 == 0 ? "{0:0}" : "{0:0.00}", number) 
like image 125
Gh61 Avatar answered Sep 28 '22 11:09

Gh61


An inelegant way would be:

var my = DoFormat(123.0); 

With DoFormat being something like:

public static string DoFormat( double myNumber ) {     var s = string.Format("{0:0.00}", myNumber);      if ( s.EndsWith("00") )     {         return ((int)myNumber).ToString();     }     else     {         return s;     } } 

Not elegant but working for me in similar situations in some projects.

like image 21
Uwe Keim Avatar answered Sep 28 '22 12:09

Uwe Keim