0

How to get floating point value?

For eg,3.75 I want only 75 how can I get it

24th Feb 2020, 2:24 PM
Keerthana Arumugam
Keerthana Arumugam - avatar
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!
24th Feb 2020, 2:58 PM
andriy kan
andriy kan - avatar
+ 2
In c ,float cannot be modulo with int
24th Feb 2020, 2:40 PM
Keerthana Arumugam
Keerthana Arumugam - avatar
+ 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.
24th Feb 2020, 2:36 PM
ReimarPB
ReimarPB - avatar
+ 1
Alright, then it would be: y = x % 1.0 * 100 Try that, I think it should work.
24th Feb 2020, 2:41 PM
ReimarPB
ReimarPB - avatar
0
3.75 % 1 = 0.75
24th Feb 2020, 2:26 PM
Seb TheS
Seb TheS - avatar
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; }
24th Feb 2020, 2:49 PM
Seb TheS
Seb TheS - avatar
0
Use modf from the math.h (cmath for C++) header file:- http://www.cplusplus.com/reference/cmath/modf/
24th Feb 2020, 6:59 PM
rodwynnejones
rodwynnejones - avatar