Display formatting help needed
Let's say I have a statement in my code:
For conversations sake lets say the variable happens to be 36348. On the display it reads 36348 but I would like the variable to read 3.6348. How do you go about formatting (?) the variable to include a decimal point and maintain the 4 decimal places?
Thanks in advance.
Don
term.dec(variable)
For conversations sake lets say the variable happens to be 36348. On the display it reads 36348 but I would like the variable to read 3.6348. How do you go about formatting (?) the variable to include a decimal point and maintain the 4 decimal places?
Thanks in advance.
Don
Comments
digit := variable / 10000
fraction := variable // 10000
Then output:
term.dec(digit)
term.tx('.')
term.dec(fraction)
If I remember right there was a trick for the rest of a division. Directly after the division you can use an internal variable to access the rest, so there is no need for a second division. Maybe someone else can jump in and confirm or reject this?!
Have some if statements, which output the right number of zeros before output of fraction.
PUB dec(value) | i, x '' Print a decimal number x := value == NEGX 'Check for max negative if value < 0 value := ||(value+x) 'If negative, make positive; adjust for max negative tx("-") 'and output sign i := 1_000_000_000 'Initialize divisor repeat 10 'Loop for 10 digits if value => i tx(value / i + "0" + x*(i == 1)) 'If non-zero digit, output digit; adjust for max negative value //= i 'and digit from value result~~ 'flag non-zero found elseif result or i == 1 tx("0") 'If zero digit (or only digit) output it i /= 10 'Update divisor
Here's a modified version that puts out a minimum number of digits so you get the leading zeroes:PUB decPlus(value,digits) | i, x, d '' Print a decimal number x := value == NEGX 'Check for max negative if value < 0 value := ||(value+x) 'If negative, make positive; adjust for max negative tx("-") 'and output sign i := 1_000_000_000 'Initialize divisor repeat d from 10 to 1 'Loop for 10 digits result |= d =< digits 'Force leading zeroes when needed if value => i tx(value / i + "0" + x*(i == 1)) 'If non-zero digit, output digit; adjust for max negative value //= i 'and digit from value result~~ 'flag non-zero found elseif result or i == 1 tx("0") 'If zero digit (or only digit) output it i /= 10 'Update divisor
Note that this is intended to be added to FullDuplexSerial. If it's just put into your program, add "term." in front of any "tx(" and you may need to change the "tx(" to "out(" depending on what display driver you're using.Try
term.dec(value/10000) term.out(".") decPlus(value//10000,4)
or something similar.http://obex.parallax.com/objects/search/?q=decimal+place&csrfmiddlewaretoken=ee6f6d6ce953648986730609c3fa7a4f
result |= d =< digits
to
if d =< digits result~~ tx(".")