C# 获取 3 位小数的问题

C# issue getting 3 decimal places

我在四舍五入时遇到问题。我将用户输入的两个数字相除,我总是得到“0”或整数。

public static void GetRatios()//This method attains the users ratio inputs
{
    double ratioCalc = waistMeasurment / heightMeasurment; //Calculates the ratio
    ratio = Math.Round((Decimal)ratioCalc, 3, MidpointRounding.AwayFromZero); //Round the answer to 3 decimal places 'ratio' is initialised in the class
}

这可能是因为 waistMeasurmentheightMeasurmentint

将它们转换为 double,您也会在 double 中得到结果。

您只需要将其中一个转换为 double 即可进行浮点除法(就像@doctor 在评论中写的那样):

double ratioCalc = (double)waistMeasurment / heightMeasurment;