Coder Perfect

To two decimal places, format a float

Problem

I’m working on a sales module for a client’s website right now. I’ve gotten the sale price to calculate fine so far, but I’m having trouble formatting the result to two decimal places.

I’m now calling this from a variable so that the results may be data binded to a listview.

Sale = float.Parse(((x.Sale_Price - (x.Sale_Price * (x.Discount_Price / 100))).ToString())),

Is it possible for someone to show me how to format the output to two decimal places? Thank you very much!

Asked by Callum

Solution #1

The format can be sent to the ToString method, for example:

myFloatVariable.ToString("0.00"); //2dp Number

myFloatVariable.ToString("n2"); // 2dp Number

myFloatVariable.ToString("c2"); // 2dp currency

Format Strings for Numbers

Answered by WraithNath

Solution #2

The first step is to change the prices’ decimal type to decimal instead of float. Because float cannot accurately represent most decimal fractions, it is unsuitable for this purpose.

Decimal once you’ve done that. To round to two decimal places, use Round().

Answered by Michael Borgwardt

Solution #3

String.Format(“{0:#,###.##}”, value)

String Formatting in C#: A more complicated example:

Answered by alexandrul

Solution #4

I believe:

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

Should do it.

Examples of C# Link String Format

Answered by Bit

Solution #5

If you want to use interpolated strings, use this method. I’m actually sharing this because I’m weary of going through the trial and error process and eventually scrolling through hundreds of documents every time I need to format a scalar.

$"{1234.5678:0.00}"        "1234.57"        2 decimal places, notice that value is rounded
$"{1234.5678,10:0.00}"     "   1234.57"     right-aligned
$"{1234.5678,-10:0.00}"    "1234.57   "     left-aligned
$"{1234.5678:0.#####}"     "1234.5678"      5 optional digits after the decimal point
$"{1234.5678:0.00000}"     "1234.56780"     5 forced digits AFTER the decimal point, notice the trailing zero
$"{1234.5678:00000.00}"    "01234.57"       5 forced digits BEFORE the decimal point, notice the leading zero
$"{1234.5612:0}"           "1235"           as integer, notice that value is rounded
$"{1234.5678:F2}"          "1234.57"        standard fixed-point
$"{1234.5678:F5}"          "1234.56780"     5 digits after the decimal point, notice the trailing zero
$"{1234.5678:g2}"          "1.2e+03"        standard general with 2 meaningful digits, notice "e"
$"{1234.5678:G2}"          "1.2E+03"        standard general with 2 meaningful digits, notice "E"
$"{1234.5678:G3}"          "1.23E+03"       standard general with 3 meaningful digits
$"{1234.5678:G5}"          "1234.6"         standard general with 5 meaningful digits
$"{1234.5678:e2}"          "1.23e+003"      standard exponential with 2 digits after the decimal point, notice "e"
$"{1234.5678:E3}"          "1.235E+003"     standard exponential with 3 digits after the decimal point, notice "E"
$"{1234.5678:N2}"          "1,234.57"       standard numeric, notice the comma
$"{1234.5678:C2}"          "$1,234.57"      standard currency, notice the dollar sign
$"{1234.5678:P2}"          "123,456.78 %"   standard percent, notice that value is multiplied by 100
$"{1234.5678:2}"           "2"              :)

Performance Warning

Strings that are interpolated are sluggish. In my experience, the following is the sequence (from fast to slow):

Answered by saastn

Post is based on https://stackoverflow.com/questions/6356351/formatting-a-float-to-2-decimal-places