Help with String.format.

Help creating logic scripts for Air Manager Instruments

Moderators: russ, Ralph

Post Reply
Message
Author
User avatar
B767
Posts: 26
Joined: Sun Mar 13, 2016 10:19 am

Help with String.format.

#1 Post by B767 »

Hello. First of all I want to say big thank for your hard work and good program. I am new in programming and ask you if you can add string.format and how to using them to textbox in WiKi. For example how to take Com1 freq and using for textbox.
Thank you so much.
P.S. Sorry for my English, my native language is Russian.

flyatr
Posts: 300
Joined: Tue Dec 01, 2015 8:53 am

Re: Help with String.format.

#2 Post by flyatr »

Hi!
It just so happens that I figured that out yesterday. I wrote a small function that can be reused:

Code: Select all

function frequency_to_string(frequency)
		whole = math.floor(frequency)					-- remove the decimal places
		fract = var_round((frequency - whole)*100,2)	-- get just the decimal places and round
		return string.format("%d.%.02d",whole,fract)	-- always show zeros (like 112.00)
end
Use it like this:

Code: Select all

txt_set(my_textbox, frequency_to_string(COM1))
You can learn a lot from existing instruments. I do it all the time.

flbessa
Posts: 69
Joined: Thu Jan 28, 2016 4:07 pm

Re: Help with String.format.

#3 Post by flbessa »

Hello there...

I'm finishing up adjusting the clock I've coded some time ago to work on air manager v3.X.
On the timer, the minutes is derived by dividing secondds / 60. This may give a fraction of, let's say 35s will give me 0.5833. When I use the string format "%02.0f" it rounds up to 1, but in fact I still want to show 0. So when my timer goes beyond 30s the minutes become 1, which is not correct.
I've seen this bug in the "ATR 72-500 - Clock".

How can I format the number to just show me the number, excluding the decimals, and not round it up/down?

Thanks,
Filipe Bessa

User avatar
Corjan
Posts: 2939
Joined: Thu Nov 19, 2015 9:04 am

Re: Help with String.format.

#4 Post by Corjan »

Hey,


Math.floor is your friend here I think, if I understand your question correctly

Code: Select all

val = 0.5833
print(string.format("%02.0f", math.floor(val)))

Corjan

flbessa
Posts: 69
Joined: Thu Jan 28, 2016 4:07 pm

Re: Help with String.format.

#5 Post by flbessa »

I definitely fix the problem! Thank you very much Corjan!
I'll submit this version of the Davtron clock.

Thanks,
Filipe

Post Reply