sanjeev40084
sanjeev40084

Reputation: 9617

Decimal number doesn't save decimal places in .net

I am reading SOA webservice which returns value in decimal format. The requirement is whatever the service returns we need to display the value as it is (including decimal places). The issue i am running into is, whenever there 0 value after the decimal places after casting the 0 decimal places gets removed. e.g. in decimal1 and decimal3, it will return 123 and 123.1 getting rid of last decimal place.

decimal decimal1 =  (decimal) 123.00;  --returns 123
decimal decimal2 = (decimal) 123.01;   --returns 123.01
decimal decimal3 = (decimal) 123.10;   --returns 123.1

Is there any way i can have the decimal place without loosing the 0 value at the end? The end data type has to be decimal (or numeric value) and cannot be string. Thanks

Sanjeev

Upvotes: 0

Views: 1205

Answers (4)

Igor
Igor

Reputation: 62298

As others mentioned a decimal is a value type that does not care about how its formatted, it cares about how it persists an accurate decimal. Representing the decimal as a human readable string should be handled by the presentation layer and in that layer the decimal should be converted from the type decimal to a string type. Here you can format the decimal so it is displayed (as a string) with XX values after the decimal, is rounded, with a . as the separator or a `,' depending on the culture of your user, etc.

See the Microsoft Standard Numeric Formats Strings web site which shows many of the built existing ways that you can use to format a decimal (among other types).

What you will need to do is use the N / Number format specifier.

decimal decimal1 =  123M;
var myFormattedString = decimal1.ToString("N2");

Upvotes: 0

Lucas Trzesniewski
Lucas Trzesniewski

Reputation: 51430

Your issue is that 123.10 is a double, and doubles don't retain precision information like decimals do.

So the underlying representation of 123.10 is equal to 123.1.
==> The cast to decimal happens too late.

The solution is to declare a decimal literal in your source code by using the m suffix.

var decimal1 = 123m;
var decimal2 = 123.10m;

decimal1.ToString() // gives "123";
decimal2.ToString() // gives "123.10";

Upvotes: 3

CodingGorilla
CodingGorilla

Reputation: 19872

What you are worried about here is how the decimal is formatted into a string, you can control this with the various overrides of .ToString(). The "data' of the decimal value remains intact regardless of how it's output as a string.

Upvotes: 0

Andrey
Andrey

Reputation: 60105

If you want to retain digits then you need to convert from string:

Convert.ToDecimal("1.10")
Convert.ToDecimal("1.1")

Because it is float who is not keeping them.

Float doesn't "remember" decimal digits. During compilation time all decimal float literals are converted to their binary counterparts and decimal 0-s are lost:

csharp> BitConverter.GetBytes(1.1)
{ 154, 153, 153, 153, 153, 153, 241, 63 }
csharp> BitConverter.GetBytes(1.10)
{ 154, 153, 153, 153, 153, 153, 241, 63 }

Upvotes: 0

Related Questions