Click here to Skip to main content
15,893,644 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi there, how can i round off a floating number to an int (non floating value)

E.g 22.69 To 23
19.7 To 20
16.5 To 17

17.4 To 17
17.2 To 17

I have tried to cast float or double to int but i don't get the desired results

Foristance,i have tried

double number = 19.7;

int number2=(int)(number)

number2=19;
This is not what i want to achieve

Thanks in advance

Yours Martin
Posted
Comments
Sergey Alexandrovich Kryukov 22-Jul-11 18:09pm    
Your operation using a type case is not rounding -- not at all.
--SA

Try using Math round
 
Share this answer
 
You need to use Math.Round(value, MidpointRoundingMode)[^]. You need to use the AwayFromZero option.

Make sure you read the entire page before you go screwing up your code.
 
Share this answer
 
Comments
DominicZA 22-Jul-11 14:29pm    
This is not entirely correct. He should just need to parse the float in as a parameter.
System.Math.Round(myNumber);

That's right I think.
 
Share this answer
 
v3
Comments
DominicZA 22-Jul-11 14:05pm    
Sorry, if you saw my my unedited answer :P I was typing from my phone!!
Math.Round() - rounds the way you tell it to
Math.Ceiling() - rounds up to nearest whole number
Math.Floor() - rounds down to the nearest whole number
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900