43

So I've got a project I'm working on. This is the only error I have:

Cannot implicitly convert type 'float' to 'int'.

I understand somewhat what that means. I just need help converting my float to int.

This is just an example of one of the floats:

float key = 0.5f;
int key = 53;

Here's the specific code section:

// price in scrap, e.g. 29 / 9 = 3.33 ref
static int BuyPricePerTOD = 21;
// price in scrap, e.g. 31 / 9 = 3.55 ref
static float SellPricePerTOD = BuyPricePerTOD + 0.5F;

static int BuyPricePerKey = 53;
static float SellPricePerKey = BuyPricePerKey + 0.5F;

static int TimerInterval = 170000;
static int InviteTimerInterval = 2000;

int UserWeapAdded,UserScrapAdded,UserRecAdded,UserRefAdded,
    UserKeysAdded,UserTODAdded,BotTODsAdded,BotKeysAdded,
    BotScrapAdded,BotRecAdded,BotRefAdded,InventoryMetal,
    InventoryScrap,InventoryRec,InventoryRef,InventoryKeys,
    InventoryTOD,PreviousTODs,PreviousKeys,WhileLoop,InvalidItem = 0;

float UserMetalAdded, BotMetalAdded, OverpayNumKeys,
    OverpayNumTOD, ExcessInScrapKey, ExcessInScrapTOD = 0.0F;
double ExcessRefinedKey, ExcessRefinedTOD = 0.0;
8
  • 1
    Could you show some code that you have made so far?
    – Edper
    Commented Feb 20, 2014 at 2:04
  • 3
    @user3325320.. int _value=Convert.ToInt32(key);
    – Revan
    Commented Feb 20, 2014 at 2:06
  • 2
    Do you want to round? Or do you just want to truncate? For example 1.8 rounded is 2, but truncated is 1. Commented Feb 20, 2014 at 2:08
  • pastebin.com/pktckAYB That is the section that needs converting. Also the number needs to be exact. I can't round it or truncate. But then again im not using decimals. Commented Feb 20, 2014 at 2:10
  • 1
    @user3325320, ints can't store fractional values, hence the name integer
    – pcnThird
    Commented Feb 20, 2014 at 2:15

2 Answers 2

67

Firstly, there are integers and floating-point numbers. Integers are always whole numbers, such as 0, 1, -32, 42 and 1337. On the other hand, floating-point numbers can have a fractional part: 0, 1, -32.1, 42.7 and 123.456788 are all valid floating-point numbers.

When converting between integers (int) and floating-point (float) numbers, you can do this:

int someInt = 42;
float someFloat = someInt;  // 42.0f

But you can't do this:

float someFloat = 42.7f;
int someInt = someFloat;    // ?

The reason the first conversion is possible, is that converting the integer number (int) to a floating-point number (float) does not change the number. It is a safe conversion, and therefore can be done implicitly.

The reason the second conversion is not allowed, is that converting the floating-point number (which may have a fractional part) to an integer number (that never has a fractional part) must drop the fractional part of the number, i.e. it becomes a different number. This is not safe, and can therefore only be done explicitly.


To explicitly convert one type of number to another, you use a cast. That's the parentheses before the number with the type of the number that you want to convert it to.

float someFloat = 42.7f;
int someInt = (int)someFloat;               // 42

Note that the fractional part of the floating-point number was dropped. It's as if it has been rounded towards zero. If you want to round the floating-point number to the nearest whole number, use the Math.Round method.

float someFloat = 42.7f;
int someInt = (int)Math.Round(someFloat);   // 43
1
  • 10
    'rounded down towards zero' is an oxymoron. I mean this is not the same thing. Your explanation is ambiguous because 'rounding down' and 'rounding towards zero' are the opposite directions for negative numbers. they are the same thing for positive numbers. Commented Aug 21, 2019 at 5:14
15

Try this :

int numInt = (int)Math.Ceiling(numFloat);

msdn documentation

You may want Math.Round() or Math.Floor() by the way.

Example :

float numFloat = 1.5f;
int testCeiling = (int)Math.Ceiling(numFloat);
int testFloor = (int)Math.Floor(numFloat);
int testRound = (int)Math.Round(numFloat);

Console.WriteLine("testCeiling = {0}", testCeiling.ToString());
Console.WriteLine("testFloor = {0}", testFloor.ToString());
Console.WriteLine("testRound= {0}", testRound.ToString());

output :

testCeiling = 2
testFloor = 1
testRound= 2

Not the answer you're looking for? Browse other questions tagged or ask your own question.