C# 用于计算其所认为的货币金额的代码’;这很奇怪。(意外小数不断出现)

C# 用于计算其所认为的货币金额的代码’;这很奇怪。(意外小数不断出现),c#,C#,我刚开始通过代码学院学习C#。我应该写一个程序来计算达到指定数量所需的不同值的最小“硬币”数量。按照说明进行操作时一切都很顺利,但在练习结束时,我们鼓励您编写更多代码,使程序能够使用十进制输入(而不仅仅是整数) 我基本上复制了用于完整金额的相同代码,只做了一些修改(将初始金额乘以100),这样它仍然可以运行并给出所需的结果但是,由于某种原因,最后一个值(铜币美分)一直给我带小数的数字。我曾想过使用Math.Floor(),但经过几次尝试后,我意识到它并不总是多余的。有人能提供一些帮助吗?Math

我刚开始通过代码学院学习C#。我应该写一个程序来计算达到指定数量所需的不同值的最小“硬币”数量。按照说明进行操作时一切都很顺利,但在练习结束时,我们鼓励您编写更多代码,使程序能够使用十进制输入(而不仅仅是整数)

我基本上复制了用于完整金额的相同代码,只做了一些修改(将初始金额乘以100),这样它仍然可以运行并给出所需的结果但是,由于某种原因,最后一个值(铜币美分)一直给我带小数的数字。我曾想过使用Math.Floor(),但经过几次尝试后,我意识到它并不总是多余的。有人能提供一些帮助吗?Math.Floor()命令是否有我应该知道的固有限制?我刚才是不是做了个大蠢事

tl;dr:noob程序员在这里,想知道为什么代码不做我想做的事情

}
}

切勿对货币使用双倍。使用
十进制

当表示物理量(如长度、质量、速度等)时,使用
double
,其中不重要的表示错误无关紧要。使用
decimal
表示货币,其中每一分钱或每一分钱的分数都很重要

区别在于:
double
将数字表示为分母为2的幂的分数,如“5/32nds”
decimal
将数字表示为分母为十的幂的分数,如“3/100”。前者倾向于积累分数(如“3/100分”)的表示错误,但后者不会

如果您的教程建议您在涉及金钱的计算中使用
double
算法,获得更好的教程


综上所述,如果在非整数量上使用
%
运算符,我怀疑您可能会误解它的用途。它主要用于整数,所以如果你在小数或双精度上使用它,可能会发生一些奇怪的事情。

千万不要在货币上使用双精度。使用
十进制

当表示物理量(如长度、质量、速度等)时,使用
double
,其中不重要的表示错误无关紧要。使用
decimal
表示货币,其中每一分钱或每一分钱的分数都很重要

区别在于:
double
将数字表示为分母为2的幂的分数,如“5/32nds”
decimal
将数字表示为分母为十的幂的分数,如“3/100”。前者倾向于积累分数(如“3/100分”)的表示错误,但后者不会

如果您的教程建议您在涉及金钱的计算中使用
double
算法,获得更好的教程

综上所述,如果在非整数量上使用
%
运算符,我怀疑您可能会误解它的用途。它主要用于整数量,所以如果你在小数或双精度上使用它,可能会发生一些奇怪的事情。

“做个大哑巴”真的吗?“做个大哑巴”真的吗?
using System;

namespace MoneyMaker
 {
class MainClass
{
public static void Main(string[] args)
{
  // This code is meant to divide an (user given) amount of money into coins of different values.

  // First we ask for an input.
  Console.WriteLine("Welcome to Money Maker!.00");
  Console.WriteLine("Please enter the amount you want to divide in Coins:");
  string strAmount = Console.ReadLine();
  Console.WriteLine($"${strAmount} Canopy is equal to:");
  double wholeAmount = Convert.ToDouble(strAmount);

  // These are the values of each coin.
  // The cents are multiplied by 100 for the purposes of using then in the code.
  // The bronze coins are listed, but unused, since their value is essentially 1.
  double gold = 10;
  double silver = 5;
  //double bronze = 1;
  double smolGold = .1 * 100;
  double smolSilver = .05 * 100;
  //double smolBronze = .01 * 100;

  // These lines calculate the integral values (gold, silver and bronze coins).
  double douAmount = Math.Floor(wholeAmount);
  double goldCoins = Math.Floor(douAmount / gold);
  double silAmount = douAmount % gold;
  double silverCoins = Math.Floor(silAmount / silver);
  double bronzeCoins = silAmount % silver;

  // These lines calculate the decimal values (gold, silver and bronze cents).
  // They start by multiplying the cents by 100, rendering the rest of the lines the same as in the integral values.
  double smolAmount = 100 * (wholeAmount - douAmount);
  double goldCents = Math.Floor(smolAmount / smolGold);
  double littleSilver = smolAmount % smolGold;
  double silverCents = Math.Floor(littleSilver / smolSilver);
  //ERROR: There seems to be an issue with the bronze cents, returning a value with decimals.
  double bronzeCents = littleSilver % smolSilver;

  // Finally, the output string with the results:
  Console.WriteLine($"\n Gold Coins: {goldCoins} \n Silver Coins: {silverCoins} \n Bronze Coins: {bronzeCoins} \n Gold Cents: {goldCents} \n Silver Cents: {silverCents} \n Bronze Cents: {bronzeCents}");
}