PDA

View Full Version : round double



wirasto
15th December 2009, 04:37
I'm looking for double round for Qt, but not lucky. qRound only round to int.

I want round function like in postgresql

select round(2.5275862068965517, 2); //2.53

dbzhang800
15th December 2009, 05:09
I'm looking for double round for Qt, but not lucky. qRound only round to int.

I want round function like in postgresql

select round(2.5275862068965517, 2); //2.53

I cannot figure out what are you doing.
But maybe what you need is a string "2.53" instead of a number 2.53 .
If so, you can easily achieve it through QString.

wirasto
15th December 2009, 08:17
Fine. So how to make 2.5275862068965517 become 2.53 with QString ?

dbzhang800
15th December 2009, 08:32
Fine. So how to make 2.5275862068965517 become 2.53 with QString ?

QString::number( 2.5275862068965517, 'f', 2);

or

QString QString::arg ( double a, int fieldWidth = 0, char format = 'g', int precision = -1, const QChar & fillChar = QLatin1Char( ' ' ) ) const

QString & QString::sprintf ( const char * cformat, ... )

In addition, you can also use functions in ansi C or C++ library, such as snprintf ,stringstream etc.