sp_m
sp_m

Reputation: 2707

Convert string value into decimal with proper decimal points

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?

Upvotes: 2

Views: 40694

Answers (6)

Bob Vale
Bob Vale

Reputation: 18474

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.

Upvotes: 4

Ankush Jain
Ankush Jain

Reputation: 1527

this will work perfectly

string getnumber = "11.10";
decimal decinum = Math.Round(Convert.ToDecimal(getnumber), 2);

Upvotes: 2

Sora
Sora

Reputation: 2551

    string getnumber = "11.10";
    double decinum = double.Parse(getnumber);

Upvotes: 0

SeanCocteau
SeanCocteau

Reputation: 1876

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.

Upvotes: 1

Diego
Diego

Reputation: 36136

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.

Upvotes: 1

Avner Shahar-Kashtan
Avner Shahar-Kashtan

Reputation: 14700

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.

Upvotes: 1

Related Questions