Jack
Jack

Reputation: 43

Displaying decimal after number

I am trying to display the following number, 40.0 by using this code:

Text{
 id: txt
 property int number: 40.0
 text: number
 x:94
 y:213
 font.family: "Helvetica"
 font.bold:true
 font.pointSize: 60
 color:"#fff"
        }

For some reason, only 40 is displaying, and no decimal. These are the other things I've tried so far:

    1.
str = QString::number(flat, 'f', 40.0);
    2.
var german = Qt.locale("de_DE");
var d;
d = Number.fromLocaleString(german, "40.0")   // d == 40.0
    3.
Text {
 Number(40.0).toLocaleString(Qt.locale("de_DE"))
}

Upvotes: 2

Views: 2930

Answers (1)

NG_
NG_

Reputation: 7173

  • Since you are working with real numbers, type should be real.
  • In QML you can use JS's toFixed() to format a number using fixed-point notation.
  • To bind property value to a function you should do as follows:
Text {
    id: txt
    property real number: 40.1
    text: {
        return txt.number.toFixed(1)
    }
}

Upvotes: 1

Related Questions