Integers and reals
Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller
Integers and reals
I have a function that returns the integer component of a real; e.g. passing a variable containing 5.2 should return 5. I want to use the value returned as a key for retrieving an element from an array. The function appears to extract the integer correctly but when the value is returned it appears as a REAL. For example, the value 5 is returned as 5.0. This then fails to locate the correct array element. As a quick fix I tried to ROUND the value in the variable but the result was still a REAL. Is this what I should be expecting? If so, how does one use a calculated value as a key for the purpose of retrieving an element from an array?
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Hi Alistair,
Here's another method to get just the integer part of a floating point number.
Just set the 'itemDelimiter' local property to a period, and return the first item.
Hope this helped,
Jan Schenkel.
Here's another method to get just the integer part of a floating point number.
Code: Select all
on mouseUp
repeat while it is not a number
ask "What number?"
if it is empty then exit mouseUp
end repeat
set the itemdelimiter to "."
answer item 1 of it
end mouseUp
Hope this helped,
Jan Schenkel.
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
-
- VIP Livecode Opensource Backer
- Posts: 977
- Joined: Sat Apr 08, 2006 7:47 am
- Contact:
Come to think of it, why not use the built-in 'trunc' function instead of building your own?
Oh, and in the interest of giving you multiple solutions - also look at the 'numberFormat' local property. This will help determine the number of decimals when converting a number to a string.
The important trick here is to append an empty string to your rounded number, to coerce Revolution into converting the rounded number to a string with the current numberFormat.
Hope this helped,
Jan Schenkel.[/i]
Oh, and in the interest of giving you multiple solutions - also look at the 'numberFormat' local property. This will help determine the number of decimals when converting a number to a string.
Code: Select all
function asInteger pNumber
set the numberFormat to "9" -- no decimals
return round(pNumber) & ""
end asInteger
Hope this helped,
Jan Schenkel.[/i]
Quartam Reports & PDF Library for LiveCode
www.quartam.com
www.quartam.com
Hi Jan
Appending the empty string was the key piece of information that I was missing. Thanks for pointing that out. My script is now retrieving data from the array.
I have also changed to using "trunc". The project I am working on was originally developed by someone else in Hypercard so I have been working with existing code.
Appending the empty string was the key piece of information that I was missing. Thanks for pointing that out. My script is now retrieving data from the array.
I have also changed to using "trunc". The project I am working on was originally developed by someone else in Hypercard so I have been working with existing code.