2

i have value stored in string format & i want to convert into decimal.

ex:

i have 11.10 stored in string format when i try to convert into decimal it give me 11.1 instead of 11.10 .

I tried it by following way

string getnumber="11.10";
decimal decinum=Convert.ToDecimal(getnumber);

i tried this also

decinum.ToString ("#.##");

but it returns string and i want this in decimal.

what could be the solution for this?

sp_m
  • 2,647
  • 8
  • 38
  • 62
  • look in to this question-http://stackoverflow.com/questions/4264736/convert-string-to-decimal-c-sharp – Buzz Aug 29 '12 at 09:04
  • 1
    what is the difference between `11.1` `11.10` `11.100000000000` ? None...someone missed some classes :) – Renatas M. Aug 29 '12 at 09:04
  • 1
    @Reniuz - One assumes the only difference is the accuracy (number of decimal points) - this may be important information to preserve in some applications. – Oded Aug 29 '12 at 09:06
  • use `decinum.ToString("0.00");` to get `11.10`, otherwise `11.1 = 11.10` – Habib Aug 29 '12 at 09:07

6 Answers6

4

As already commented 11.1 is the same value as 11.10

decimal one=11.1;
decimal two=11.10;
Console.WriteLine(one == two);

Will output true

The # formatter in the to string method means an optional digit and will supress if it is zero (and it is valid to suppress - the 0 in 4.05 wouldn't be suppressed). Try

decinum.ToString("0.00"); 

And you will see the string value of 11.10

Ideally you actually want to use something like

string input="11.10";
decimal result;

if (decimal.TryParse(input,out result)) {
   Console.WriteLine(result == 11.10);
} else {
  // The string wasn't a decimal so do something like throw an error.
}

At the end of the above code, result will be the decimal you want and "true" will be output to the console.

Bob Vale
  • 18,094
  • 1
  • 42
  • 49
  • Just a small annotation for German readers: The call has to be `decinum.ToString("0.00");`, even if the localized decimal point is comma. "0,00" will not work. "0.00" will result in the string being correctly localized to "11,10". – Daniel Marschall Jul 06 '18 at 10:51
2

this will work perfectly

string getnumber = "11.10";
decimal decinum = Math.Round(Convert.ToDecimal(getnumber), 2);
Ankush Jain
  • 1,532
  • 1
  • 15
  • 24
1

A decimal datatype stores a value. The value of 11.1 is identical to that of 11.10 - mathemtically, they're the exact same number, so it's behaving properly.

What you seem to want is to display the number as 11.10, but that's up to the code that displays it - I don't know if you're writing to log file, displaying on a web-page or any other format - but that is independent of the internal representation of the decimal datatype.

Avner Shahar-Kashtan
  • 14,492
  • 3
  • 37
  • 63
1

there is no solution, This is expected behaviour. 11.10 in string = 11.1 in number

you cant store zeros on the decimal part, otherwise 11.10 would be different than 11.100, which is true if you are talking about strings but not if you are talking about numbers.

I think your problem is on a presentation level only. Why dont you explain better what you want to do.

Diego
  • 34,802
  • 21
  • 91
  • 134
1

11.10 expressed as a decimal is 11.1, just like it is 11.100000000000000000000000000000000000.

For mathematical processes, don't worry about how it displays itself. If you are displaying the value then converting it into a string is no biggie either. Remember that

decinum.ToString ("#.##");

is returning a string (from the 'ToString' function) and not converting the 'decinum' to a string.

SeanCocteau
  • 1,838
  • 19
  • 25
0
    string getnumber = "11.10";
    double decinum = double.Parse(getnumber);
Sora
  • 2,465
  • 18
  • 73
  • 146