当我使用带有2位小数的双倍和十进制数进行简单减法时,例如2.45,总和的结果最多包含15位小数 [英] When I do simple subtraction with a double and a decimal number with 2 decimal places e.g 2.45 , The result of the sum has up to 15 decimal places

查看:62
本文介绍了当我使用带有2位小数的双倍和十进制数进行简单减法时,例如2.45,总和的结果最多包含15位小数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 

双重变化;
int twoEuro = 0,oneEuro = 0,50Cent = 0,20Cent = 0,tenCent = 0,5Cent = 0,twoCent = 0,oneCent = 0;

Console.WriteLine(" Enter in change");
change = double.Parse(Console.ReadLine());

int euros =(int)change * 100;

欧元=欧元/ 100;

双倍美分=变化 - 欧元;


而(欧元!= 0)
{
如果(欧元> = 2)
{
欧元=欧元 - 2 ;
twoEuro ++;

}

else
{

euros = euros - 1;
oneEuro ++;
}


}

do
{
if(cents> = 0.50)
{
美分=美分 - 0.50;
50Cent ++;
Console.WriteLine(美分);
}

否则如果(美分> = 0.20&&美分< 0.50)
{

美分=美分 - 0.20;
twentyCent ++;
Console.WriteLine(美分);
}

否则如果(美分> = 0.10&&美分< 0.20)
{

美分=美分 - 0.10;
tenCent ++;
Console.WriteLine(美分);
}

否则如果(美分> = 0.05&&美分< 0.10)
{

美分=美分 - 0.05;
fiveCent ++;
Console.WriteLine(美分);
}

否则如果(美分> = 0.02&&美分<0.05)
{
美分=美分 - 0.02;
twoCent ++;
Console.WriteLine(美分);

}

其他
{
美分=美分 - 0.01;
oneCent ++;
Console.WriteLine(美分);
}
} while(美分> = 1);


Console.WriteLine("你的变化= {0}两欧元,{1}一欧元,{2}五十美分,{3} 20美分,{4} 10美分,{5} 5美分,{6} 2美分,{7} 1美分",
两个欧元,一个欧元,五十个,二十个,十个,五个,两个实体,一个实体);
}


我正在尝试编写一个简单的程序,允许用户输入一笔金额 -  
程序然后应该显示最低金额所需的金额


例例7.89 - 要求
3 - 2欧元硬币,1 - 1欧元硬币,1 - 50c,1 - 20c,1 - 10c,1 - 5c,2 - 2c。每次从中取出一笔金额后,我输出变量美分中的金额,以便看到美分的新值。

找到欧元的变化工作正常,我无法找到分数的变化。

当我输入例如4.56时,输出如下。

0.0599999999999996
您的更改= 2欧元,0欧元,1 50美分,0 20美分,0 10美分,0 5美分,0 2美分,0 1美分。

为什么只进行一次减法然后中断do while循环?为什么变化中有17个小数位?

解决方案

使用。 ToString
重载以格式化输出


https://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-up-to-2-places-或-简单整数




double change; int twoEuro = 0, oneEuro = 0, fiftyCent = 0, twentyCent = 0, tenCent = 0, fiveCent = 0, twoCent = 0, oneCent = 0; Console.WriteLine("Enter in change "); change = double.Parse(Console.ReadLine()); int euros = (int)change * 100; euros = euros / 100; double cents = change - euros; while (euros != 0) { if (euros >= 2) { euros = euros - 2; twoEuro++; } else { euros = euros - 1; oneEuro++; } } do { if (cents >= 0.50) { cents = cents - 0.50; fiftyCent++; Console.WriteLine(cents); } else if (cents >= 0.20 && cents < 0.50) { cents = cents - 0.20; twentyCent++; Console.WriteLine(cents); } else if (cents >= 0.10 && cents < 0.20) { cents = cents - 0.10; tenCent++; Console.WriteLine(cents); } else if (cents >= 0.05 && cents < 0.10) { cents = cents - 0.05; fiveCent++; Console.WriteLine(cents); } else if (cents >= 0.02 && cents < 0.05) { cents = cents - 0.02; twoCent++; Console.WriteLine(cents); } else { cents = cents - 0.01; oneCent++; Console.WriteLine(cents); } } while (cents >= 1); Console.WriteLine("Your change = {0} two euros, {1} one euros, {2} fifty cents, {3} 20 cents, {4} 10 cents, {5} 5 cents, {6} 2 cents, {7} 1 cent", twoEuro, oneEuro, fiftyCent, twentyCent, tenCent, fiveCent, twoCent, oneCent); }

I'm  trying to write a simple program to allow the user to enter an amount of money – the
program should then display the least amount of coins required to make
up the sum.

Example 7.89 – requires 3 – 2 euro coins, 1 – 1 euro coin, 1 – 50c,1 – 20c,1 – 10c,1 – 5c,2- 2c.

I output the amount in the variable cents after each time an amount is taken away from it in order to
see the new value of cents.

Finding the Euros change is working OK, I am having trouble finding the change for the cents.

When I enter for example 4.56, The output will be as follows.

0.0599999999999996
Your change = 2 two euros, 0 one euros, 1 fifty cents, 0 20 cents, 0 10 cents, 0 5 cents, 0 2 cents, 0 1 cents.

Why does it only do one subtraction then break out of the do while loop?
why is there 17 decimal places in change?

解决方案

Use .ToString overload to format the output

https://stackoverflow.com/questions/6951335/using-string-format-to-show-decimal-up-to-2-places-or-simple-integer


这篇关于当我使用带有2位小数的双倍和十进制数进行简单减法时,例如2.45,总和的结果最多包含15位小数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆