0

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;
samoncode
  • 466
  • 2
  • 7
  • 23
  • 2
    How is `value` and `value1` declared? – AnT stands with Russia Nov 13 '12 at 06:52
  • When you get `2,5E-02` do you print `2` or `0`? If you print `2` then make sure you locale is `C` (`.` is probably not the decimal point in your locale.) http://stackoverflow.com/questions/1333451/c-locale-independent-atof – vladr Nov 13 '12 at 06:56
  • @AndreyT now I feel stupid. I declared the values as double and it works. Sorry to bother you all! – samoncode Nov 13 '12 at 07:07
  • If you set the locale correctly. Put `std::cout.imbue(std::locale(""));` as the first line in main. Then `operator>>` will decode comma separated numbers (if that is what you expect in your locale as set in your computers settings), – Martin York Nov 13 '12 at 07:25

2 Answers2

1

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

Aki Suihkonen
  • 19,144
  • 1
  • 36
  • 57
  • Thank you for your effort! But like I wrote in the comment it was just me being stupid. I had to declare the value/value1 variable as double. The problem with the decimal-character I solved with the replace-function. – samoncode Nov 13 '12 at 07:16
1

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.

Phlucious
  • 3,704
  • 28
  • 61