How to make 1.123456 , 1.123?
-
float b = 1.123456
int a = b * 1000 // a = 1123And after that you'd better work with integers. If you then write something like
b = float(a) / 1000
you will get b ~ 1.1230000045 (or even 1.122999999) because it is impossible to represent decimals as binaries exactly
-
Since you didn't say if your trying to get a string or just round the floating point number, I'm going to assume you want to get a string.
float fl = 1.1234;
printf("%.3f", fl); // only uses the first 3 numbers after the decimal
this prints to consoleuse to make a cstr of it
sprintf(cstr destination, cstr to be formatted, args...); -
My wrong not saying what exactly i want.. here the comment and tell me if you think my solution is bad...
@float number= 3.333333 // here it could be 1/ 1.3333333/ 1.33 .. What i want is always to show 5 numbers.. so 1->1.000 / 1.3333333->1.333 / 1.33->1.330
QString mpu=QString::number(number);
if(mpu.count()==1)
mpu=mpu+".000";
else if(mpu.count()==3)
mpu=mpu+"00";
else if(mpu.count()==4)
mpu=mpu+"0";ui->lcdNumber->display(mpu.left(5));@
-
[quote author="mlong" date="1348759875"]Why not just use
@
QString::number(value,'f',5);
@
to force 5 decimal places?
[/quote]i said 3 decimal places.. so 5->3 will do exactly what i want.. thank you a lot! i really need to read my C++ book but i don't have time cause of my studies.. next year :)
-
Leon: So you plan to nag us here for one more year because you have no time to learn something due to studies? I am not sure that is what is intended with this whole university-thing:-)