Unity C# 调用在以下方法或属性之间不明确:"System.Math.Round(double, int)"和"System.Math.Round(decimal, int)



这是我的代码。我收到错误

调用在以下方法或属性之间不明确:System.Math.Round(double, int)System.Math.Round(decimal, int)

我知道这是由于操作的结果在双精度和小数之间模棱两可,但我不知道如何只获得小数作为结果。

int enemiesThisWave= Decimal.ToInt32(Math.Round(TotalEnemies * (percentForWave / 100), 1));
int enemiesForType = Decimal.ToInt32(Math.Round(lenghtList * (percentForType / 100), 1));

执行此操作的一种简单方法是将其中一个值强制转换为要调用的类型:

int enemiesThisWave = Decimal.ToInt32(Math.Round(TotalEnemies * ((decimal)percentForWave / 100), 1));

或在100值上指定小数:

int enemiesThisWave = Decimal.ToInt32(Math.Round(TotalEnemies * (percentForWave / 100m), 1));

假设TotalEnemiespercentForWaveint

改用Mathf.FloorToInt

int enemiesThisWave= Mathf.FloorToInt(TotalEnemies * percentForWave / 100f + 0.5f);
int enemiesForType = Mathf.FloorToInt(lenghtList * percentForType / 100f + 0.5f);

最新更新