Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Float to Int [closed]

Tags:

c#

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;
like image 482
jerseyetr Avatar asked Feb 20 '14 02:02

jerseyetr


People also ask

What happens when you convert a float to int?

Convert a float to an int always results in a data loss. The trunc() function returns the integer part of a number. The floor() function returns the largest integer less than or equal to a number. The ceil() function returns the smallest integer greater than or equal to a number.

How do I change Dtype from float to int?

The df. astype(int) converts Pandas float to int by negelecting all the floating point digits. df. round(0).

How do you convert float to int in Python?

A float value can be converted to an int value no larger than the input by using the math. floor() function, whereas it can also be converted to an int value which is the smallest integer greater than the input using math. ceil() function. The math module is to be imported in order to use these methods.

Can we convert float to int in Java?

Java allows you to cast, or convert a variable of one type to another; in this case, cast a float to an int. This option removes all digits to the right of the decimal place. Another option is to use the Math. round method to round the value to the nearest integer.


2 Answers

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
like image 132
Daniel A.A. Pelsmaeker Avatar answered Oct 02 '22 23:10

Daniel A.A. Pelsmaeker


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
like image 30
aloisdg Avatar answered Oct 02 '22 22:10

aloisdg