-7

In c# when adding two decimals the program will automatically get rid of the number 0 after the decimal places.

For example adding 0.50 to 1.20 will produce the answer of 1.7 and this is annoying because i need to display this answer in terms of money.

Is there a way to prevent this?

  • 4
    You display it with a format. Post your code. – LarsTech Mar 21 '18 at 16:16
  • 2
    what you really want to know is how to _display_ it with 2 decimal places. C# does not store the trailing 0 inside the decimal object, because, for storage and maths purposes, it's redundant. It's purely a human convention to add that zero when displaying it as currency. Lots of examples already online of how to do that. In the meantime try to separate, conceptually in your mind, the processing and storage of the data from the manner in which it is presented to the user. – ADyson Mar 21 '18 at 16:17
  • 2
    Decimal isn't money. Decimal is decimal. You need custom formatting if you want to convert a number into a string the way you describe. Look up how to format a decimal to a number of decimal places. There have been many questions on that subject. – ProgrammingLlama Mar 21 '18 at 16:17
  • bookmark this page https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings – Jonesopolis Mar 21 '18 at 16:20
  • when dealing with money, i usually go to the lower integral value possible (cents) do my math with integers then i format the output as needed, i also save in the database as integer. :-) – Zorkind Mar 21 '18 at 16:30
  • Possible duplicate of [C# Display 2 decimal places](https://stackoverflow.com/questions/17999701/c-sharp-display-2-decimal-places) – Rufus L Mar 21 '18 at 16:54
  • 1
    @Zorkind That's totally acceptable if you are working with one predetermined currency. _But_ there are currencies with standard 2 decimal places ( the familiar $ / Cent, € / Cent ... ) and there are currencies without "lower" and currencies with default 3 places "after the dot" ... (been there, done that :/ ) – Fildor Mar 21 '18 at 16:54
  • @Fildor i see your point, but, most major credit card gateways use this method tho. And they support multiple currencies using that same system. Actually that's why they store integers. – Zorkind Mar 21 '18 at 16:57
  • cents are always cents, it can't be larger than 2 digits, if it is, then it's not a cent, like i said, you need to grab the lower possible monetary unit. Formatting it will always be just a simple math for cents (value / 100) and so on. – Zorkind Mar 21 '18 at 17:00
  • @Zorkind No, 3 places wouldn't be cent. Correct. I also didn't say "you can't" - all I am saying is "it is not _always_ that simple". – Fildor Mar 21 '18 at 17:04
  • @Fildor Apologies, i stand corrected :-) – Zorkind Mar 21 '18 at 17:12

3 Answers3

2

If you want to display your Decimal with two decimal places, please use :

myDecimal.ToString("N2");

You may want to take a look at Standard Numeric Format Strings for more information.

Onyx Caldin
  • 289
  • 1
  • 12
1
decimal d = 0.50m;
decimal d1 = 1.20m;
Console.Write(d+d1);

Please find this Post

Babai
  • 99
  • 1
  • 14
0

I'm not sure about if you mean this, but you can try the toString() method in currency format this way:

double number = 1.2;
string numberCurrency = number.ToString("C");
Console.WriteLine(numberCurrency); //this prints "1.20"

I recommend you to read this https://msdn.microsoft.com/es-es/library/kfsatb94(v=vs.110).aspx

Fildor
  • 14,510
  • 4
  • 35
  • 67
jacdDev
  • 196
  • 1
  • 8
  • 1
    A few corrections: `WriteLine` has a capital `L`, `ToString` has a capital `T`, and the output also adds the currency symbol (in the U.S. the output is: `$1.20`). I won't upvote an answer with code that doesn't compile... :) – Rufus L Mar 21 '18 at 16:47