0

I have a Text item that display a real number:

Text { id: txt; ... }

...

txt.text: Math.round(sensor.value*10)/10

...

But if sensor.value is the integer number, it's displayed without a fractional part (35 instead 35.0). How to explicitly force txt always display a fractional part of number?

ntrsBIG
  • 39
  • 4

2 Answers2

0

Use toPrecision method to specify the number of decimals you want to show. The method will return a string with the number formatted in the desired format

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_objects/Number/toPrecision

VictorArcas
  • 630
  • 2
  • 7
  • 23
0

You can use:

var num = 5.11;
num.toFixed(1);

It will round up to 1 place of decimal.

Anand Vaidya
  • 609
  • 2
  • 16
  • 41