neoMetalero
neoMetalero

Reputation: 68

Unity C# The call is ambiguous between the following methods or properties: `System.Math.Round(double, int)' and `System.Math.Round(decimal, int)

This is my code. I'm getting the error

The call is ambiguous between the following methods or properties: System.Math.Round(double, int) and System.Math.Round(decimal, int)

I know this is due to the result of the operation being ambiguous between a double and a decimal, but I have no clue on how to get only a decimal as a result.

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

Upvotes: 0

Views: 1927

Answers (2)

Ruzihm
Ruzihm

Reputation: 20249

Use Mathf.FloorToInt instead:

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

Upvotes: 1

JSteward
JSteward

Reputation: 7091

One simple way to do this is to cast one of your values to the type you want to invoke:

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

or specify decimal on your 100 value:

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

Assuming TotalEnemies and percentForWave are int

Upvotes: 4

Related Questions