Old 08-14-2019, 09:42 AM   #1
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default 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 ...
DarkStar is offline   Reply With Quote
Old 08-14-2019, 10:16 AM   #2
Xenakios
Human being with feelings
 
Xenakios's Avatar
 
Join Date: Feb 2007
Location: Oulu, Finland
Posts: 8,062
Default

Quote:
Originally Posted by DarkStar View Post

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.
Xenakios is offline   Reply With Quote
Old 08-14-2019, 10:26 AM   #3
Lokasenna
Human being with feelings
 
Lokasenna's Avatar
 
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,551
Default

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.
__________________
I'm no longer using Reaper or working on scripts for it. Sorry. :(
Default 5.0 Nitpicky Edition / GUI library for Lua scripts / Theory Helper / Radial Menu / Donate
Lokasenna is offline   Reply With Quote
Old 08-14-2019, 11:35 PM   #4
Tale
Human being with feelings
 
Tale's Avatar
 
Join Date: Jul 2008
Location: The Netherlands
Posts: 3,646
Default

JSFX:

Code:
// Efficient
frac = x - (x|0);

// Might be more precise for large numbers.
frac = x >= 0 ? x - floor(x) : x - floor(x) - 1;
Tale is offline   Reply With Quote
Old 08-15-2019, 01:31 AM   #5
DarkStar
Human being with feelings
 
DarkStar's Avatar
 
Join Date: May 2006
Location: Surrey, UK
Posts: 19,677
Default

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 ...
DarkStar is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:43 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.