Domanda

I've got a routine which gets numbers in the exponent format(e.g. 2,5E-02 or 4E+06) as a QString. When I print the values I always only get integers and when they are smaller then 1 I always get 0. Does anyone know what I am doing wrong here? (with the cout line I only wanted to test whether the QString::number() is ruining it for me)

here is a code snippet:

QStringList valPair;
value = atof(valPair[0].replace(",",".").toAscii());
value1 =atof(valPair[1].replace(",",".").toAscii());
strValue = "[" + QString::number(value) + ", " + QString::number(value1) + "]";
//cout<<value<<" "<<value1;

I'd appreciate any help!

EDIT: It was a Problem with variable declaration...

double value, value1;
È stato utile?

Soluzione

Why are you doing all that work? Qt already has what you're looking for if you use QString::toDouble and QString::number(). If you set your locale manually before calling toDouble() then you can use the comma decimal notation without replacing anything.

You could also create a string template like QString("[%1,%2]") and then use the double version of QString::arg.

Altri suggerimenti

The conversion function doesn't support your locale, which uses comma as a decimal separator. Use 2.4e4 instead.

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top