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/projects/mepres/book7/bk7i17/bk7_17i1.htm
i need to display 4 and 0.276
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/projects/mepres/book7/bk7i17/bk7_17i1.htm
i need to display 4 and 0.276
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:
double d = 23.4;
double w = (int)d; /* w == 23.0 */
double p = d - w; /* p == 0.4 */
just do type casting
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:
double d = 23.4; double w = (int)d; /* w == 23.0 */ double p = d - w; /* p == 0.4 */
DAMN thats common sense
THanks alot.
idk what that means
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)
.
im sorry but i havent learned that in my class
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)
.
i need help
I need to display
Example
23.4
and display 23 and .4how 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.
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
int prec = 5;
int nf = (int)( ( n - nw ) * pow( 10, prec ) );
But this method is not precise.
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.
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
int prec = 5; int nf = (int)( ( n - nw ) * pow( 10, prec ) );
But this method is not precise.
It needs to be the precision the user defines the float to be.
I'm trying to create a function in the format
void func1( char *c1, float f1, float f2, float f3 )
that will simulate another function in the format
void func2( char *c1, int i1, int i2, int i3, int i4, int i5, int i6 )
by pulling apart the float arguments and passing them as int arguments (e. g., 1, 2, 3, 4, 5, 6 instead of 1.2, 3.4, 5.6).
Even if I use sprintf() for the fraction part of the number, I still need to know the length of the number for precision so I don't get a bunch of trailing garbage, right? For example wouldn't the following code yield trailing garbage on char *str?
char *str;
int n = 123.456;
sprintf( str, "%f" n );
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.