Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Decimal ToString() conversion issue in C#

Tags:

string

c#

decimal

I am facing a problem when I try to convert decimal? to string. Scenario is

decimal decimalValue = .1211;
string value = (decimalValue * 100).ToString();

Current Result : value = 12.1100

Expected Result : value = 12.11

Please let me know, what could be reason for this.

like image 644
Pushkar Avatar asked May 15 '13 10:05

Pushkar


People also ask

How do you convert decimal to ToString?

To convert a Decimal value to its string representation using a specified culture and a specific format string, call the Decimal. ToString(String, IFormatProvider) method.

How do you fix decimals in C#?

Normally, we need to output the decimal values to 2 precision numbers. However, sometimes we also need to restrict the decimal variable itself to store not more than 2 decimal values (for example -12.36). The following are two possible ways to convert a decimal to a string and also restrict it to 2 decimal places.

What does ToString C mean?

The "C" (or currency) format specifier is used to convert a number to a string representing a currency amount. Let us see an example. double value = 139.87; Now to display the above number until three decimal places, use (“C3”) currency format specifier. value.ToString("C3", CultureInfo.CurrentCulture)


1 Answers

Decimal preserves any trailing zeroes in a Decimal number. If you want two decimal places instead:

decimal? decimalValue = .1211m;
string value = ((decimal)(decimalValue * 100)).ToString("#.##")

http://msdn.microsoft.com/en-us/library/0c899ak8.aspx

or

string value = ((decimal)(decimalValue * 100)).ToString("N2")

http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx

From System.Decimal:

A decimal number is a floating-point value that consists of a sign, a numeric value where each digit in the value ranges from 0 to 9, and a scaling factor that indicates the position of a floating decimal point that separates the integral and fractional parts of the numeric value.

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28. Therefore, the binary representation of a Decimal value is of the form, ((-296 to 296) / 10(0 to 28)), where -296-1 is equal to MinValue, and 296-1 is equal to MaxValue.

The scaling factor also preserves any trailing zeroes in a Decimal number. Trailing zeroes do not affect the value of a Decimal number in arithmetic or comparison operations. However, >>trailing zeroes can be revealed by the ToString method if an appropriate format string is applied<<.

Remarks:

  1. the decimal multiplication needs to be casted to decimal, because Nullable<decimal>.ToString has no format provider
  2. as Chris pointed out you need to handle the case that the Nullable<decimal> is null. One way is using the Null-Coalescing-Operator:

    ((decimal)(decimalValue ?? 0 * 100)).ToString("N2")
    

This article from Jon Skeet is worth reading:

Decimal floating point in .NET (seach for keeping zeroes if you're impatient)

like image 200
Tim Schmelter Avatar answered Sep 18 '22 16:09

Tim Schmelter