getting the last number, not a string - LUA Scripting - CHDK Forum supplierdeeply

getting the last number, not a string

  • 6 Replies
  • 2269 Views
*

Offline Mlapse

  • *****
  • 584
  • S95 S110
getting the last number, not a string
« on: 13 / March / 2022, 08:12:17 »
Advertisements
was trying to pull the last digit from a number and tried with:
string.sub(time_in_seconds, i)

and while this works in extracting the last digit i cannot use it in a IF statement with a number since it is a string.

after looking over the documentation i could not find the proper code to do the same with a number or to change the string into a number.
do we have a solution for this in lua that i have overlooked?

i think i have :) let's try tonumber()
apparantly this was a rhetorical question.
« Last Edit: 13 / March / 2022, 08:51:26 by Mlapse »
frustration is a key ingredient in progress

*

Offline c_joerg

  • *****
  • 1251
Re: getting the last number, not a string
« Reply #1 on: 13 / March / 2022, 12:13:04 »
You could try a bitwise operations like

Code: [Select]
X=17
Y=bitand(X,1)
M100 100a, M3 121a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd

*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Re: getting the last number, not a string
« Reply #2 on: 13 / March / 2022, 14:09:29 »
sweet, saves another 2 lines of code  :)
frustration is a key ingredient in progress

*

Offline reyalp

  • ******
  • 14120
Re: getting the last number, not a string
« Reply #3 on: 13 / March / 2022, 16:31:41 »
and while this works in extracting the last digit i cannot use it in a IF statement with a number since it is a string.
You can use tonumber() on the resulting string. You can use the modulo operator %. For example
Code: [Select]
X%10
gives you the final decimal digit.

You could try a bitwise operations like

Code: [Select]
X=17
Y=bitand(X,1)
This will give you the last bit, which is not the same as the last decimal digit.
Don't forget what the H stands for.


*

Offline c_joerg

  • *****
  • 1251
Re: getting the last number, not a string
« Reply #4 on: 13 / March / 2022, 17:08:46 »
This will give you the last bit, which is not the same as the last decimal digit.
Yes, of course. 
« Last Edit: 13 / March / 2022, 17:12:31 by c_joerg »
M100 100a, M3 121a, G9x II (1.00c), 2*G1x (101a,100e), S110 (103a), SX50 (100c), SX230 (101a), S45,
Flickr https://www.flickr.com/photos/136329431@N06/albums
YouTube https://www.youtube.com/channel/UCrTH0tHy9OYTVDzWIvXEMlw/videos?shelf_id=0&view=0&sort=dd

*

Offline Mlapse

  • *****
  • 584
  • S95 S110
Re: getting the last number, not a string
« Reply #5 on: 13 / March / 2022, 18:21:25 »
thanks reyalp, much better :)
frustration is a key ingredient in progress

*

Offline Caefix

  • *****
  • 947
  • Sorry, busy deleting test shots...
Re: getting the last number, not a string
« Reply #6 on: 14 / March / 2022, 09:57:47 »
If only the first number is needed: :)
Code: [Select]
function cap(x)
  while (math.abs(x)>9)  x = x/10 end
  return x
end
All lifetime is a loan from eternity.

 

Related Topics


SimplePortal 2.3.6 © 2008-2014, SimplePortal