How can I display a number in QML with its sign, even if it is +?

Viewed 31

//I know this code does the job for positive numbers but not for all numbers.

text: qsTr ("+") + (some_positive_number)

//Is there a way that I can always display any number with its sign beside it?

2 Answers

This should work fine in standard javascript:

(a - b > 0 ? "+" : "") + (a - b)

Note that:

  1. There is little to no sense in using qsTr for + since its meaning is fairly localised and internationalised by default.

  2. No need to calculate the difference of a - b thrice.

text: (a - b > 0) ? (qsTr("+") + (a - b)) : (a - b)

//This will work fine.

Related