Format strings and numbers

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Is it possible to format a numeric value where it does not round the value, ie if i try to format 2.59 to 1 decimal place using Console.WriteLine(2.59M.ToString("0.0")); i get 2.6, i have looked in the SDK docs and have read the following paragraph which explains the above behaviour, if anyone knows how to get 2.5 in the above example i would appreciate the help

Digit placeholder. Displays a digit or a zero. If the expression has a digit in the position where the zero appears in the format string, display it; otherwise, displays a zero in that position.
If the number has fewer digits than there are zeros (on either side of the decimal) in the format expression, displays leading or trailing zeros. If the number has more digits to the right of the decimal separator than there are zeros to the right of the decimal separator in the format expression, rounds the number to as many decimal places as there are zeros. If the number has more digits to the left of the decimal separator than there are zeros to the left of the decimal separator in the format expression, displays the extra digits without modification

Thanks in advance
Pat
 
PMcG said:
Is it possible to format a numeric value where it does not round the value, ie if i try to format 2.59 to 1 decimal place using Console.WriteLine(2.59M.ToString("0.0")); i get 2.6, i have looked in the SDK docs and have read the following paragraph which explains the above behaviour, if anyone knows how to get 2.5 in the above example i would appreciate the help.

I think you'll have to perform the truncation yourself:

(Decimal.Floor(10M * 2.59M)/10M).ToString("0.0")

You'll probably want to encapsulate the truncation code into a nice
little method)
 
Back
Top