-2

First of all this is not a duplicate question my need is to have 2 precision decimal always

e.g.
2 should be converted as 2.00
0 should be converted as 0.00
0.5 should be converted as 0.50

output number has to be decimal

Things i had tried

decimal val = 0.5000M
decimal d = Math.Round(val, 2);//gives 0.5 i need 0.50

NOTE: I am not looking to convert decimal to string as i need to send it as a soap request which accepts decimal value only, so i dont have flexibility to convert it to string.

Update below code as answered by Jakub Lortz works fine though it may look complex

solution 1 by Jakub Lortz

decimal twoPoint00 = new Decimal(200, 0, 0, false, 2); decimal twoPoint0 = new Decimal(20, 0, 0, false, 1); Console.WriteLine(twoPoint00) // prints 2.00 Console.WriteLine(twoPoint0) // prints 2.0

solution 2 by Bogdan

he had given very simple solution which will also work
decimal val = 0.5000M;
string result = string.Format("{0:f2}", val); decimal d; Decimal.TryParse(result, out d);

Anshul Nigam
  • 1,608
  • 1
  • 12
  • 26
  • Do you want as numeric value or as string? – Arghya C Dec 11 '15 at 16:37
  • Your issue seems more about how/where the value is displayed, rather than the actual precision itself. Where are you using the output? – Chris Dec 11 '15 at 16:37
  • actually i wanted to send it as a part of soap request where field is specified as decimal , so i want value as numeric to be precise decimal – Anshul Nigam Dec 11 '15 at 16:38
  • Its not duplicate as above link deal with representing it in string, and i want it to be shown in decimal itself. – Anshul Nigam Dec 11 '15 at 16:41
  • 6
    When it's decimal, `0.5` and `0.50` are exactly the same. When displaying somewhere (that is a `string`), there is a difference. – Arghya C Dec 11 '15 at 16:42
  • @AnshulNigam I don't think you ever display decimals... It's always converted to a string upon being displayed. And if you are not displaying it, wherever you are sending it too needs to deal with trailing 0s. – Ryan Searle Dec 11 '15 at 16:48
  • guys, please try to understand requirement, i can't convert it to string as i need to send it as soap request , also as it involves money calculations it is hard requirement to have to 2 decimal place. – Anshul Nigam Dec 11 '15 at 16:48
  • 1
    @AnshulNigam That doesn't make sense. `0.5` and `0.50` are exactly the same number. The only difference is how they're displayed, which is absolutely a property of a string. – Rob Dec 11 '15 at 16:48
  • 3
    can you explain why returning the decimal `0.5` is incorrect when it is the same as `0.50`? TBH I think you are trying to deal with this in the wrong place - you need to deal with this on the presentation level. – user1666620 Dec 11 '15 at 16:48
  • 4
    @AnshulNigam Your requirement does not make sense. The service should recognize `0.5` and `0.50` as the *exact same number*. If it doesn't, then the service is ***fundamentally*** broken and your *only* option is to send it a string representing the number. The only other thing you could do is rounding (to fix cases such as `0.501`), which you have already sorted out yourself – Rob Dec 11 '15 at 16:49
  • I can't see where your soap request would recognize any difference between a `decimal` of value 2.5 and a _different_ (???) `decimal` of value 2.50 – René Vogt Dec 11 '15 at 16:51
  • I agree with @RenéVogt and AnshulNigam. The system should understand that 2.5 and 2.50 is the same number – Kyle Williamson Dec 11 '15 at 16:55
  • Ok, so you're saying that you need to send this as part of a SOAP request. What technology are you using to communicate with the server? It seems your need pertains more to controlling how that SOAP request is created, rather than the details of the .NET decimal datatype. Although, it seems rather silly if a web service can't accept "2.5". – Dr. Wily's Apprentice Dec 11 '15 at 16:56
  • A down vote ? can somebody explain why? – Anshul Nigam Dec 11 '15 at 17:10

3 Answers3

5

There is a constructor of Decimal that allows you to specify the scale:

public Decimal(
    int lo,
    int mid,
    int hi,
    bool isNegative,
    byte scale
)

For example:

decimal twoPoint00 = new Decimal(200, 0, 0, false, 2);
decimal twoPoint0 = new Decimal(20, 0, 0, false, 1);
Console.WriteLine(twoPoint00) // prints 2.00
Console.WriteLine(twoPoint0)  // prints 2.0

This constructor is used by the C# compiler to create decimals from literals, so you can easily check it in the compiled IL.

Edit

To convert a decimal value to a representation with a set precision, you would first have to use Decimal.GetBits method to get its internal representation. Then modify the values (maybe using BigInteger to work with the whole 96-bits value as a whole?) and create a new decimal.

Seems like a lot of job, with a lot of possible bugs. I'd start by checking the solution proposed in Bogdan's answer - ToString() + Parse(). It seems to work correctly and is very simple.

Community
  • 1
  • 1
Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39
1

I guess you want to convert the decimal into a string. Try it like that:

string result = d.ToString("#.00");
René Vogt
  • 43,056
  • 14
  • 77
  • 99
1
decimal val = 0.5000M;   
string result = string.Format("{0:f2}", val);
decimal d;
Decimal.TryParse(result, out d);

@Jakub Lortz - Thank you for the suggestions:

decimal val = 0.5000M;   
string formattedVal = val.ToString("F2"); 
decimal result = Decimal.Parse(formattedVal);
rm -rf .
  • 473
  • 1
  • 4
  • 14
  • parsing a decimal to a string to a decimal is ridiculous – user1666620 Dec 11 '15 at 16:53
  • Do you think it works? – Arghya C Dec 11 '15 at 16:56
  • Thanks bogdan it worked. – Anshul Nigam Dec 11 '15 at 17:41
  • @AnshulNigam in this code, what (which variable) holds your desired result? – Arghya C Dec 11 '15 at 17:57
  • 1
    Nice and simple. I'd suggest some small changes though. We don't need `string.Format`, `ToString()` is enough. And since we create the string, we don't need to worry about `Decimal.Parse` throwing an exception. So `string formattedVal = val.ToString("F2"); decimal result = Decimal.Parse(formattedVal);` is enough. – Jakub Lortz Dec 11 '15 at 20:40
  • "@Arghya C @AnshulNigam in this code, what (which variable) holds your desired result?" - If you need the result as a STRING you have to use 'result', if you need a DECIMAL type you need to use 'd'. – rm -rf . Dec 14 '15 at 11:40