Login or Sign Up
Logging in...
Remember me
Log in
Or
Sign Up
Forgot password or user name?
Log in with
Search in titles only
Search in C Sharp only
Search
Advanced Search
Forums
Product Launch
Updates
Today's Posts
Member List
Calendar
Home
Forum
Topic
C Sharp
Convert Floating Point numbers to integer
Collapse
X
Collapse
Posts
Latest Activity
Photos
Page
of
1
Filter
Time
All Time
Today
Last Week
Last Month
Show
All
Discussions only
Photos only
Videos only
Links only
Polls only
Events only
Filtered by:
Clear All
new posts
Previous
template
Next
Bhavs
New Member
Join Date:
Sep 2008
Posts:
16
#1
Convert Floating Point numbers to integer
Jan 6 '09, 01:43 PM
Hi...
Can anyone help me with the code to convert 4E+07 to 40,000,000 in c#
Thanks,
Bh...
Plater
Recognized Expert
Expert
Join Date:
Apr 2007
Posts:
7872
#2
Jan 6 '09, 02:36 PM
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
[code=c#]
//Need to append F to store it in a float percision
float myfloat = 4E+07F;
//or you could store it as a double
double mydouble=4E+07;
int myint=(int)myfl oat;
//or
int myint=(int)mydo uble;
[/code]
Comment
Post
Cancel
Bassem
Contributor
Join Date:
Dec 2008
Posts:
344
#3
Jan 6 '09, 09:08 PM
Hello Bhavs,
Type Casting like Plater said,
float x = 12.8;
int n = (int) x;
or
int n = ConvertToInt32( x);
Comment
Post
Cancel
uogen
New Member
Join Date:
Jan 2009
Posts:
18
#4
Jan 7 '09, 07:52 AM
HI ,Bassem
why you use 12.8?
represent what?
Comment
Post
Cancel
Bassem
Contributor
Join Date:
Dec 2008
Posts:
344
#5
Jan 7 '09, 02:27 PM
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
Comment
Post
Cancel
r035198x
MVP
Join Date:
Sep 2006
Posts:
13225
#6
Jan 7 '09, 02:36 PM
Of course the solutions given are correct only if the floating point number is less than the largest int.
Comment
Post
Cancel
Bassem
Contributor
Join Date:
Dec 2008
Posts:
344
#7
Jan 7 '09, 02:39 PM
Sure, r035198x. Otherwise we may use long int64.
Comment
Post
Cancel
r035198x
MVP
Join Date:
Sep 2006
Posts:
13225
#8
Jan 8 '09, 06:57 AM
Originally posted by
Bassem
Sure, r035198x. Otherwise we may use long int64.
Which also has a maximum value ...
Comment
Post
Cancel
Previous
template
Next
Working...
Yes
No
OK
OK
Cancel
👍
👎
☕
Comment