943,614 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 5548
  • C RSS
You are currently viewing page 1 of this multi-page discussion thread
Jul 27th, 2009
0

int to float to float to int

Expand Post »
i need help
I need to display
Example
23.4
and display 23 and .4

how do i do this?

so i need to convert a decimal into fraction
like this
http://www.cimt.plymouth.ac.uk/proje...7/bk7_17i1.htm
i need to display 4 and 0.276
Last edited by yasaswyg; Jul 27th, 2009 at 4:48 pm. Reason: found out
Similar Threads
Reputation Points: 6
Solved Threads: 0
Light Poster
yasaswyg is offline Offline
43 posts
since Jul 2009
Jul 27th, 2009
0

Re: int to float to float to int

Quote ...
I need to display
Example
23.4
and display 23 and .4
One easy way is with the type system. When you cast a floating point value it cuts off the precision. If you subtract that result from the original, you get the precision:
  1. double d = 23.4;
  2. double w = (int)d; /* w == 23.0 */
  3. double p = d - w; /* p == 0.4 */
Last edited by Tom Gunn; Jul 27th, 2009 at 5:01 pm.
Reputation Points: 1446
Solved Threads: 135
Practically a Master Poster
Tom Gunn is offline Offline
681 posts
since Jun 2009
Jul 27th, 2009
0

Re: int to float to float to int

just do type casting
Reputation Points: 34
Solved Threads: 7
Posting Whiz in Training
MrNoob is offline Offline
218 posts
since May 2009
Jul 27th, 2009
0

Re: int to float to float to int

Click to Expand / Collapse  Quote originally posted by Tom Gunn ...
One easy way is with the type system. When you cast a floating point value it cuts off the precision. If you subtract that result from the original, you get the precision:
  1. double d = 23.4;
  2. double w = (int)d; /* w == 23.0 */
  3. double p = d - w; /* p == 0.4 */
DAMN thats common sense
THanks alot.
Reputation Points: 6
Solved Threads: 0
Light Poster
yasaswyg is offline Offline
43 posts
since Jul 2009
Jul 27th, 2009
0

Re: int to float to float to int

idk what that means
Reputation Points: 6
Solved Threads: 0
Light Poster
yasaswyg is offline Offline
43 posts
since Jul 2009
Jul 28th, 2009
0

Re: int to float to float to int

you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:

'd' is a double (floating point) variable containing 23.4.

'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.

'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'

now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)


.
Last edited by jephthah; Jul 28th, 2009 at 12:32 am. Reason: .
Reputation Points: 2143
Solved Threads: 178
Posting Maven
jephthah is offline Offline
2,567 posts
since Feb 2008
Jul 28th, 2009
0

Re: int to float to float to int

im sorry but i havent learned that in my class

Click to Expand / Collapse  Quote originally posted by jephthah ...
you dont know what what means? casting? start from the beginning of Tom Gunns code snippet:

'd' is a double (floating point) variable containing 23.4.

'w' is a double variable set to the value of 'd' once 'd' is casted as an integer. (see where 'd' is casted as an integer by the use of 'int' type in parentheses.) now 'w' contains the whole value 23.0 with no fractional part.

'p' is another double variable that is the value of the difference between 'd' and 'w'... since 'd' is 23.4 and 'w' is 23.0, the difference is 0.4, and this fractional part is assigned to 'p'

now 'd' still contains the original decimal value (23.4), and 'w' contains the whole number (23) and 'p' contains the fractional part (0.4)


.
Reputation Points: 6
Solved Threads: 0
Light Poster
yasaswyg is offline Offline
43 posts
since Jul 2009
Aug 17th, 2009
0

Re: int to float to float to int

Click to Expand / Collapse  Quote originally posted by yasaswyg ...
i need help
I need to display
Example
23.4
and display 23 and .4

how do i do this?
I'm trying to do something similar, only instead of displaying the numbers, I need to store them. I need to store both as ints, though.

The problem I'm running into is coming up with a generic algorithm for storing the 0.4 part as just 4. I don't know how many digits the number will be every time, so I can't use a constant multiplier to move the decimal.

This is what I've come up with so far:

float n = 123.456;

// 123
int nw = (int)n;

// 456
int nf = (int)( ( n - nw ) * pow( 10, ??? ) );

I would much prefer a mathematical solution, but I would be willing to convert to a string and back, if that's the only way.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
42Wired is offline Offline
13 posts
since Aug 2009
Aug 17th, 2009
0

Re: int to float to float to int

Floating type does not have fixed precision.
and if you enter
123.456
try to print that variable - you'll get something like that
123.455969706597869578956
So you have to convert its number into string and parse it.

Or choose precision and your code will be like that
cpp Syntax (Toggle Plain Text)
  1. int prec = 5;
  2. int nf = (int)( ( n - nw ) * pow( 10, prec ) );
But this method is not precise.
Last edited by Protuberance; Aug 17th, 2009 at 2:06 pm.
Reputation Points: 78
Solved Threads: 17
Junior Poster in Training
Protuberance is offline Offline
88 posts
since Aug 2009
Aug 17th, 2009
0

Re: int to float to float to int

Is there a better (faster) way to convert than sprintf() that you know of? I'm working in a real-time system, and sprintf() is computationally expensive.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
42Wired is offline Offline
13 posts
since Aug 2009

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C Forum Timeline: modify the "save as" function to "save"
Next Thread in C Forum Timeline: seperate float's decimal portion





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC