By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,984 Members | 1,461 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,984 IT Pros & Developers. It's quick & easy.

Convert Floating Point numbers to integer

P: 16
Hi...

Can anyone help me with the code to convert 4E+07 to 40,000,000 in c#

Thanks,
Bh...
Jan 6 '09 #1
Share this Question
Share on Google+
7 Replies


Plater
Expert 5K+
P: 7,872
If the number is already in a float or double datatype, just type cast it down to an int.

EDIT:
After seeing the example below, I suppose I should provide one too
Expand|Select|Wrap|Line Numbers
  1.  
  2. //Need to append F to store it in a float percision
  3. float myfloat = 4E+07F;
  4. //or you could store it as a double
  5. double mydouble=4E+07;
  6.  
  7. int myint=(int)myfloat;
  8. //or
  9. int myint=(int)mydouble;
  10.  
  11.  
Jan 6 '09 #2

100+
P: 344
Hello Bhavs,

Type Casting like Plater said,

float x = 12.8;
int n = (int) x;
or
int n = ConvertToInt32(x);
Jan 6 '09 #3

uogen
P: 18
HI ,Bassem
why you use 12.8?
represent what?
Jan 7 '09 #4

100+
P: 344
Hello uogen
It represents nothing except a floating point number, any number i meant. So, you can convert a float number to integer by truncate the fraction.

Regards,
Bassem
Jan 7 '09 #5

10K+
P: 13,264
Of course the solutions given are correct only if the floating point number is less than the largest int.
Jan 7 '09 #6

100+
P: 344
Sure, r035198x. Otherwise we may use long int64.
Jan 7 '09 #7

10K+
P: 13,264
@Bassem
Which also has a maximum value ...
Jan 8 '09 #8

Post your reply

Sign in to post your reply or Sign up for a free account.