PDA

View Full Version : Formatting for two decimal places



TomJoad
4th April 2011, 16:50
Hello everyone,

I am making a simple interest calculator. My problem is that when a number, such as the payoff total, ends in 0, that 0 doesn't show.

The code used for rounding to two decimal places:

double r_total = static_cast<double>(static_cast<int>(total*100+0.5))/100.0;

The code used for converting to a string:

QVariant tmp(r_total);
QString s_total = tmp.toString();

I've been racking my brain trying to come up with a way to show that 0, so it shows "$55.20" instead of "$55.2", but can't seem to come up with anything?

Does anyone have any ideas?

wysota
4th April 2011, 16:56
double val = 1.2;
QString str= QString::number(val, 'f', 2);