0
How to separate the elemen of float?cause I want to take 2 number behind the .
5 Respuestas
+ 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
+ 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
0
I want to separate it into 1 individual number
0
float myfloat = 34.54f;
float post_DP = myfloat-(int)myfloat;
printf("%.2f", post_DP);
//or this...
printf("%.0f", post_DP*100);
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);