PDA

View Full Version : double to QString



swiety
22nd November 2007, 11:49
729194496 B = 695.4 MB
2x695.4 MB = 145838898 B = 1.35 GB
1 GB = 2^30 B = 1073741824 B

and now


QFileInfo file("movie.avi");
qint64 size = file.size(); // 729194496 B
size += size; // 145838898 B
double z = size/1073741824;
QString str;
str = QString::number( z, 'f', 2 ); // 1.00 ( WRONG )
str.sprint("%.2f", z ); //1.00

int x = floor( z ); // x = 1, qRound( z ) = 1 ( GOOD )
int y = floor( (z - x )*100 ); // y=0 ( WRONG )
str = QString::number(x)+"."+QString::number(y); //1.00 ( WRONG )


So, how can i get 1.35 GB ?

wysota
22nd November 2007, 11:56
How about:

qint64 GB = 1024*1024*1024;
QString str = QString::number(((double)file.size())/((double)(GB)));

bender86
22nd November 2007, 15:11
double z = size/1073741824;
size is an integer. Result of a division of two integers is another integer. You should do

double z = ((double)size) / 1073741824;

Better is the way suggested by wysota.