C# 用数学取整。取整

C# 用数学取整。取整,c#,C#,我想知道是否有办法使用Math.Round函数将值0.05美分转换为5美分?我知道我可以通过将值乘以100来实现这一点,但我不想这样做,因为这些值存储在数组中,在执行其他函数时会破坏我的其他计算 例如,如果该值小于1美元,则我希望打印出不带小数点的分值,请参考以下代码: double[] coins = {0.05,0.10,0.20,0.50,1.00,2.00}; Console.WriteLine("Change is as follows:"); for (int j

我想知道是否有办法使用Math.Round函数将值0.05美分转换为5美分?我知道我可以通过将值乘以100来实现这一点,但我不想这样做,因为这些值存储在数组中,在执行其他函数时会破坏我的其他计算

例如,如果该值小于1美元,则我希望打印出不带小数点的分值,请参考以下代码:

double[] coins = {0.05,0.10,0.20,0.50,1.00,2.00};

Console.WriteLine("Change is as follows:");
        for (int j = 0; j < change_given.Length; j++)
        {
            if (change_given[j] == 0)
            {
                continue;
            }
            if (coins[j] < 1)
            {
                Console.WriteLine("{0} x {1}c", change_given[j], coins[j]);
            }
            else
            {
                Console.WriteLine("{0} x ${1}", change_given[j], coins[j]);
            }
        }
double[]硬币={0.05,0.10,0.20,0.50,1.00,2.00};
Console.WriteLine(“更改如下:”);
对于(int j=0;j
看起来这只是为了显示。如果将
硬币[j]*100相乘,则不会更新硬币[j]:

    for (int j = 0; j < change_given.Length; j++)
    {
        if (change_given[j] == 0)            
            continue;            
        if (coins[j] < 1)
            Console.WriteLine("{0} x {1}c", change_given[j], coins[j]*100);
        else
            Console.WriteLine("{0} x ${1}", change_given[j], coins[j]);
    }
for(int j=0;j
看起来这只是为了显示。如果将
硬币[j]*100相乘,则不会更新硬币[j]:

    for (int j = 0; j < change_given.Length; j++)
    {
        if (change_given[j] == 0)            
            continue;            
        if (coins[j] < 1)
            Console.WriteLine("{0} x {1}c", change_given[j], coins[j]*100);
        else
            Console.WriteLine("{0} x ${1}", change_given[j], coins[j]);
    }
for(int j=0;j
目标:以整数打印美元和美分。源是一个双精度数组。无需更改原始数组本身,只需读取其成员以进行显示

预期产出

变化如下:
5美分
10美分
20美分
50美分
1美元
1美元42美分
2美元
代码

using System;

public class Test
{
    public static void Main()
    {
        double[] coins = {0.05,0.10,0.20,0.50,1.00,1.42,2.00};

        Console.WriteLine("Change is as follows:");

        for (int j = 0; j < coins.Length; j++)
        {
            var amount = coins[j];
            var dollars = Math.Floor(amount);
            var change = amount - dollars;
            var cents = 100*change;

            string ds = dollars == 1 ? String.Empty : "s";
            string cs = cents == 1 ? String.Empty : "s";

            if (amount >= 0 && amount < 1)
            {
                Console.WriteLine("{0} cents", cents);
            }
            else if (dollars >= 1 && cents == 0)
            {
                Console.WriteLine("{0} dollar{1}", dollars, ds);
            }
            else
            {
                Console.WriteLine("{0} dollar{1} and {2} cent{3}",
                    dollars, ds, cents, cs);
            }
        }
    }
}
使用系统;
公开课考试
{
公共静态void Main()
{
双[]币={0.05,0.10,0.20,0.50,1.00,1.42,2.00};
Console.WriteLine(“更改如下:”);
对于(int j=0;j=0&&金额<1)
{
Console.WriteLine(“{0}美分”,美分);
}
否则,如果(美元>=1,美分==0)
{
Console.WriteLine(“{0}美元{1}”,美元,ds);
}
其他的
{
Console.WriteLine(“{0}美元{1}和{2}美分{3}”,
美元、ds、美分、cs);
}
}
}
}

目标:以整数打印美元和美分。源是一个双精度数组。无需更改原始数组本身,只需读取其成员以进行显示

预期产出

变化如下:
5美分
10美分
20美分
50美分
1美元
1美元42美分
2美元
代码

using System;

public class Test
{
    public static void Main()
    {
        double[] coins = {0.05,0.10,0.20,0.50,1.00,1.42,2.00};

        Console.WriteLine("Change is as follows:");

        for (int j = 0; j < coins.Length; j++)
        {
            var amount = coins[j];
            var dollars = Math.Floor(amount);
            var change = amount - dollars;
            var cents = 100*change;

            string ds = dollars == 1 ? String.Empty : "s";
            string cs = cents == 1 ? String.Empty : "s";

            if (amount >= 0 && amount < 1)
            {
                Console.WriteLine("{0} cents", cents);
            }
            else if (dollars >= 1 && cents == 0)
            {
                Console.WriteLine("{0} dollar{1}", dollars, ds);
            }
            else
            {
                Console.WriteLine("{0} dollar{1} and {2} cent{3}",
                    dollars, ds, cents, cs);
            }
        }
    }
}
使用系统;
公开课考试
{
公共静态void Main()
{
双[]币={0.05,0.10,0.20,0.50,1.00,1.42,2.00};
Console.WriteLine(“更改如下:”);
对于(int j=0;j=0&&金额<1)
{
Console.WriteLine(“{0}美分”,美分);
}
否则,如果(美元>=1,美分==0)
{
Console.WriteLine(“{0}美元{1}”,美元,ds);
}
其他的
{
Console.WriteLine(“{0}美元{1}和{2}美分{3}”,
美元、ds、美分、cs);
}
}
}
}

数组中的值的类型是什么?它们是数字类型还是字符串类型?从你的问题我看不出来对不起,它们是数字类型。硬币值在数组中存储为双倍。这没有任何意义。从0.05到5怎么样?向我们展示你的代码,可能会有一个不同的、更好的解决方案。这只是为了展示的目的吗?乘以100以您想要的格式显示值有什么错?用上面的代码更新了问题。数组中的值是什么类型的?它们是数字类型还是字符串类型?从你的问题我看不出来对不起,它们是数字类型。硬币值在数组中存储为双倍。这没有任何意义。从0.05到5怎么样?向我们展示你的代码,可能会有一个不同的、更好的解决方案。这只是为了展示的目的吗?乘以100以您想要的格式显示值有什么错?用上面的代码更新了问题。谢谢伙计。我刚做了这件事,它起作用了。我当时头脑一片空白。离开电脑一段时间后回来,现在已经解决了。@Maff,你用数学解决了它。回合?“怎么做?”杰克,我不是用数学来做的。考虑到这一点