0

How to separate the elemen of float?cause I want to take 2 number behind the .

25th Dec 2019, 1:49 AM
Susetyoadhi
5 Answers
+ 4
Do you really need to cut it off? Or is it only for output? double n = 6.6754335 printf("%.2lf", n); output: 6.67
25th Dec 2019, 2:03 AM
HonFu
HonFu - avatar
+ 1
I would turn it into a string and manipulate from there? Tested in Java - pretty sure it can be shorter and the logic is a bit cumbersome but maybe it can help you build your own version float myFloat = 55.25F; String floatVal = String.valueOf(myFloat); int firstNum = floatVal.length() - 2; int secondNum = floatVal.length() - 1; System.out.print( floatVal.charAt(firstNum ) ); System.out.print( floatVal.charAt(secondNum ) ); https://code.sololearn.com/c7jx3ET5Bp01/?ref=app
25th Dec 2019, 1:59 AM
HNNX 🐿
HNNX 🐿 - avatar
0
I want to separate it into 1 individual number
25th Dec 2019, 2:06 AM
Susetyoadhi
0
float myfloat = 34.54f; float post_DP = myfloat-(int)myfloat; printf("%.2f", post_DP); //or this... printf("%.0f", post_DP*100);
25th Dec 2019, 2:10 AM
rodwynnejones
rodwynnejones - avatar
0
ahh...separate (individual numbers) then:- float myfloat = 34.54f; int myfirstInt = (int)(myfloat*10)%10; int mysecondInt = (int)(myfloat*100)%10; printf("%d and %d", myfirstInt, mysecondInt);
25th Dec 2019, 2:28 AM
rodwynnejones
rodwynnejones - avatar