C # Number after decimal disappearance after I join them with string

I have the following problem: after I add a line to my number, the second digit after the point will disappear, like this

var needed_product = sets * (2 * price_bananas) + sets * (4 * price_eggs) + sets * (0.2 * price_berries); var difference = Math.Abs(needed_product - amount_cash); if (amount_cash>=needed_product) { Console.Write("Ivancho has enough money - it would cost " + "{0:F2}", needed_product + "lv."); } 

The output is 37.4, when it should be 37.40 with another decimal. How to fix this problem? When I print it without adding the last fragment of the line, the output of the integer is correct.

+5
source share
6 answers

You format your string incorrectly. Console.Write takes a string argument and then optionally takes some arguments to pass to the string.

In your sample code, your line

 "Ivancho has enough money - it would cost " + "{0:F2}" 

and then the argument you pass to this line,

 needed_product + "lv." 

needed_product added to "lv." and in the process is converted to a string without your custom parameters. This means that {0:F2} converted to a string and ignores the F2 modifier, providing

 "Ivancho has enough money - it would cost 37.4 lv." 

Use instead

 Console.Write("Ivancho has enough money - it would cost {0:F2} lv.", needed_product); 
+5
source

You can use a custom format in a call toString ().

  double value = 37.4; Console.WriteLine(value.ToString("0.00")); 
+1
source

The problem is that you combine the decimal value with the string before formatting it.

In other words, your current code is functionally equivalent:

 var temp = needed_product + "lv."; // this gets formatted using default formatting Console.Write("Ivancho has enough money - it would cost {0:F2}", temp); 

To fix this, you just need to insert the units in the line of the actual format:

 Console.Write("Ivancho has enough money - it would cost {0:F2} lv", needed_product); 
+1
source

If you run the following, fixed code:

 void Main() { var sets=6; var price_bananas=0.88; var price_eggs=1.10; var price_berries=0.37; var amount_cash=40; var needed_product = sets * (2 * price_bananas) + sets * (4 * price_eggs) + sets * (0.2 * price_berries); var difference = Math.Abs(needed_product - amount_cash); if (amount_cash >= needed_product) { Console.Write( "Ivancho has enough money - it would cost " + "{0:F2} lv.", needed_product); } } 

It produces the correct conclusion:

Ivancho has enough money - it will cost 37.40 BGN.

Explanation: Another way you tried to convert the number to a string inside (using the equivalent of the .ToString() method), and then apply the {0:F2} formatting, which became useless since the number is already a default formatted string options.

To avoid this, create a full format string, then add the parameters without changes (as shown in the code).

+1
source

You will fix this problem using integers and cents. This is the only correct way to deal with currency.

Use a function that divides by 100 and formats each displayed output.

If you have no problem using float, this will happen sooner or later.

0
source

change this value

 "{0:F2}" 

to

 "{0:0.##}" 
-2
source

Source: https://habr.com/ru/post/1247860/


All Articles