Jump to content
The simFlight Network Forums

DME Distance string.format question


Recommended Posts

I am using a  Goflight GF-166 as a DME which includes the following Lua script...

function Distance1(offset,value)
	if (value <= "100") then
		miles1 = string.format("%3.1f",value)
	else
		miles1 = string.format("%s",value)
	end
	prevswitch = -1 -- updates Distance
end

event.offset(0x0C29,"STR",4,"Distance1") -- DME1 Distance

When less than 100 miles, miles1 displays both miles and tenths/mile (e.g.,   0.5   7.5   10.5   99.9 ), as it should.

However, when more than 100 miles, miles1 displays just the miles as a whole number, but the decimal point remains (e.g., 100.  105.   115. ).

Is there a way to rid of the decimal point altogether when more than 100 miles?

Link to comment
Share on other sites

On 3/8/2024 at 8:49 PM, Delta14Sierra said:
function Distance1(offset,value)
	if (value <= "100") then

This doesn't make much sense as value is a string, and you cannot use the numeric comparison of '<=' on string values - this will NOT be doing what you think.

What you can do is check if the 4th character is a period or not, e.g.

function Distance1(offset,value)
    if (string.byte(value, 4) == 46) then -- is 4th character a period (46)?
        miles1 = string.sub(value, 1, 3)
    else
        miles1 = value
    end
    prevswitch = -1 -- updates Distance
end

event.offset(0x0C29,"STR",4,"Distance1") -- DME1 Distance

John

  • Like 1
Link to comment
Share on other sites

John,

Thank you so much for that instructional insight which now removes the period when it is the fourth character.

But then I found that my GF-166 display was showing 00.0 - 09.9 when the cockpit DME shows 0.0 - 9.9.

So I applied your insight and included an elseif statement which now also strips the leading zero.

function Distance1(offset,value) -- NM1
	if (string.byte(value, 4) == 46) then -- is 4th character a period (46)?
		miles1 = string.sub(value, 1, 3)
	elseif (string.byte(value, 1) == 48) then -- is leading character a zero (48)?
		miles1 = string.format("%1.1f",value)
	else
		miles1 = value
	end
	prevswitch = -1 -- updates Distance
end

There probably is another way to strip the leading zero, but this replicates the cockpit DME on my GF-166 which is what I was hoping to accomplish.

Thank you for sharing your expertise.

Dan

Link to comment
Share on other sites

16 minutes ago, Delta14Sierra said:

So I applied your insight and included an elseif statement which now also strips the leading zero.

Ok, but I am surprised this works because here:

16 minutes ago, Delta14Sierra said:
miles1 = string.format("%1.1f",value)

value is a string and you are treating it as a number. Maybe it is implicitly converted, but would be better to just use strings, i.e.
     miles1 = string.sub(value, 2, 4)

24 minutes ago, Delta14Sierra said:

Thank you for sharing your expertise.

No problem,

John

  • Like 1
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use. Guidelines Privacy Policy We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.