0
How to get floating point value?
For eg,3.75 I want only 75 how can I get it
7 Respostas
+ 4
in c/c++:
double x = 3.75;
double y = x - trunc(x); // y will be 0.75
there are some other variants, like
double y = x - (int)x; // if x will be in range of int
modulus operator (%) may not be applied to floating point operands!
+ 2
In c ,float cannot be modulo with int
+ 1
To add to Seb TheS 's answer:
x = 3.75
y = x % 1 * 100
Now, y would be 75. Make sure to type it in a way that fits in your programming language of choice.
+ 1
Alright, then it would be:
y = x % 1.0 * 100
Try that, I think it should work.
0
3.75 % 1 = 0.75
0
Python:
x = 3.75
y = x % 1
while y % 1:
y *= 10
C:
float x = 3.75;
float y = x % 1.0;
while (y % 1.0) {
y *= 10.0;
}
0
Use modf from the math.h (cmath for C++) header file:-
http://www.cplusplus.com/reference/cmath/modf/