|
|
|
08-14-2019, 09:42 AM
|
#1
|
Human being with feelings
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
|
Q: Get signed decimal part from number?
Given, for example, the numbers (which are themselves the result of a calculation)
-9.12, -7.45, -0.52, 0.52, 7.45, 9.12
In JS FX or lua, what is the best way to get these formatted results from them?
-12, -45, -52, +52, +45 and +12
I've tried various combos of %, floor() and ceil(), but no luck so far.
Also, do those numbers after the decimal point have a mathematical name?
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
|
|
|
08-14-2019, 10:16 AM
|
#2
|
Human being with feelings
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
|
Quote:
Originally Posted by DarkStar
Also, do those numbers after the decimal point have a mathematical name?
|
It's called the fractional part. Lua has the function math.modf that returns the first (integral) part and the fractional part.
Code:
local wholepart,fracpart = math.modf(-9.12)
__________________
I am no longer part of the REAPER community. Please don't contact me with any REAPER-related issues.
|
|
|
08-14-2019, 10:26 AM
|
#3
|
Human being with feelings
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
|
It's probably easiest to just do a string match and then cast it back to an integer.
Code:
local num = -9.12
local output = string.gsub(num, "%d+%.", "")
--> -12
If it's useful to have them separately, you can also do:
Code:
local sign, dec = string.match(num, "([^%d]?)%d+%.(%d+)
--> -, 12
Note: In either case, the output will be a string.
|
|
|
08-14-2019, 11:35 PM
|
#4
|
Human being with feelings
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,646
|
JSFX:
Code:
// Efficient
frac = x - (x|0);
// Might be more precise for large numbers.
frac = x >= 0 ? x - floor(x) : x - floor(x) - 1;
|
|
|
08-15-2019, 01:31 AM
|
#5
|
Human being with feelings
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
|
Phew! Thank you guys - I can see me using each of them in different circumstances. Tale's approach is the most appropriate for my current work
__________________
DarkStar ... interesting, if true. . . . Inspired by ...
|
|
|
Thread Tools |
|
Display Modes |
Linear Mode
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -7. The time now is 06:43 AM.
|