-4

is it possible to return decimal from web service with value 0C instead of 0M. return type must be decimal so converting to currency string option doesnt work.

decimal return value is 0M but in response it is shown as <Money>0.0000</Money> value must be in currency format i.e. 0.00

Maybe some DataAnotation would help?

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
AuBa
  • 83
  • 2
  • 9
  • 1
    What type of webservice, what is the interface, ... – H H Oct 19 '15 at 06:38
  • A `decimal` has no formatting – H H Oct 19 '15 at 06:38
  • Possible duplication [1](http://stackoverflow.com/questions/10741190/currency-formatting-mvc),[2](http://stackoverflow.com/questions/11175921/string-formatting-with-currency-double-values-not-displaying-correctly),[3](http://stackoverflow.com/questions/32957318/string-format-return-value-of-pure-method-is-not-used) – Waqar Ahmed Oct 19 '15 at 06:39
  • `0.0000` and `0.00` are equal as a `double`. – Soner Gönül Oct 19 '15 at 06:40

1 Answers1

4

I know two solutions to solve this problem.

One

Return strings:

string money = yourValue.ToString("0.00");

Two

Use Round:

decimal money = Math.Round(yourValue, 2);

Parsed

It will always add .00 to integer values.

decimal money = decimal.Parse(yourvalue.ToString("0.00"));
Fka
  • 6,044
  • 5
  • 42
  • 60
  • 1
    Funny how your Minion avatar looks left to my avatar, now that I've edited your question. I feel bad now… – Uwe Keim Oct 19 '15 at 06:46
  • 1
    Hahah :) You want that cookie? :D – Fka Oct 19 '15 at 06:47
  • first one does't wor for me couse return type must be decimal, the second one does the job converting from 0.0000 to 0.00, but what if i have initial value 2? it will still display 2 instead of currency 2.00. any ideas on that? – AuBa Oct 19 '15 at 06:52
  • Why not to combine two of these approaches? I edited question. – Fka Oct 19 '15 at 06:58