Page 1 of 1

How to remove decimal zero

Posted: Thu Apr 22, 2021 12:32 pm
by brodhaq
Hello,
I am trying to achieve the following functionality to display my TCAS range:

txt_set(txt_range, 74/milestopixels)

The problem is, that if the result is a whole number, the resulting text contains a decimal zero.

Current functionality:
result = 10 -> shown text = 10.0
result 2.5 -> shown text = 2.5

Required functionality:
result = 10 -> shown text = 10 (without decimal zero)
result 2.5 -> shown text = 2.5

Anybody has an easy solution please?
Thank you!
Pavel

Re: How to remove decimal zero

Posted: Thu Apr 22, 2021 12:43 pm
by Keith Baxter
Hi,


Are you talking about format?

_txt(tostring(string.format("%03.0f",10)--- will give "010"

_txt(tostring(string.format("%.0f",10)--- will give " 10" no leading zeros

_txt(tostring(string.format("%.1f",10)--- will give " 10.0" no leading zeros one trailing decimal


var_round(10.1,0) will give "10"
var_round(10.0,1) will give "10.0"



Keith

Re: How to remove decimal zero

Posted: Thu Apr 22, 2021 12:44 pm
by brodhaq
Hello and thank you,
I know those, but I believe they always remove the decimal, even if I want it to be displayed.

I need 2.5 to remain 2.5, but I also need 2.0 to be 2.

Pavel

Re: How to remove decimal zero

Posted: Thu Apr 22, 2021 12:49 pm
by Keith Baxter
Hi,

Ahhh

Have you tried string match?

Keith

Re: How to remove decimal zero

Posted: Thu Apr 22, 2021 1:30 pm
by Sling
If I think a bit longer there is likely something better, perhaps using Regex maybe but what about this for a top of the head idea.

var = fif(var % 1 == 0, math.floor(var), var)

Re: How to remove decimal zero

Posted: Wed May 05, 2021 1:09 pm
by brodhaq
Sling, this works so good! Thank you.

Pavel