Cockos Incorporated Forums Q: Get signed decimal part from number?
 Register Track Bugs/Feature Requests Search Today's Posts Mark Forums Read

 08-14-2019, 09:42 AM #1 DarkStar Human being with feelings     Join Date: May 2006 Location: Surrey, UK Posts: 18,025 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
Xenakios
Human being with feelings

Join Date: Feb 2007
Location: Oulu, Finland
Posts: 7,925

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)`
__________________
For info on SWS Reaper extension plugin (including Xenakios' previous extension/actions) :
http://www.sws-extension.org/
https://github.com/Jeff0S/sws
--
Xenakios blog (about HourGlass, Paul(X)Stretch and λ) :
http://xenakios.wordpress.com/

 08-14-2019, 10:26 AM #3 Lokasenna Human being with feelings     Join Date: Sep 2008 Location: Calgary, AB, Canada Posts: 6,257 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 Tale Human being with feelings     Join Date: Jul 2008 Location: The Netherlands Posts: 2,975 JSFX: Code: ```// Efficient frac = x - (x|0); // Might be more precise for large numbers. frac = x >= 0 ? x - floor(x) : x - floor(x) - 1;``` __________________ Martinic Kee Bass - Scanner Vibrato - Elka Panther - Tale's JSFX Pack
 08-15-2019, 01:31 AM #5 DarkStar Human being with feelings     Join Date: May 2006 Location: Surrey, UK Posts: 18,025 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 BB code is On Smilies are On [IMG] code is On HTML code is Off Forum Rules
 Forum Jump User Control Panel Private Messages Subscriptions Who's Online Search Forums Forums Home General Discussion     General Discussion (aka spam trap) REAPER Forums     REAPER General Discussion Forum     newbieland     REAPER Q&A, Tips, Tricks and Howto     Recording Technologies and Techniques     REAPER Compatibility     REAPER Color Themes and Icon Sets     MIDI Hardware, Control Surfaces, and OSC     REAPER Non-English Speaking User Forums         Forum de REAPER en français         Foro de REAPER en Español         Fórum do REAPER em português         Forum di REAPER in italiano         Deutschsprachiges REAPER Userforum         Pyccкоязычный фopyм REAPER     REAPER Bug Reports     REAPER Feature Requests     Dstruct's Casa De Nitpicks     REAPER for Live Use     REAPER for Video Editing/Mangling     REAPER for Ambisonic and 3D positional audio uses     ReaScript, JSFX, REAPER Plug-in Extensions, Developer Forum     REAPER for macOS X     REAPER for Linux     REAPER Pre-Release Discussion     REAPER Music/Collaboration Discussion     REAPER lounge NINJAM Discussion     NINJAM User Discussion     NINJAM Developer Discussion Other Software Discussion     WDL users forum     LICEcap Discussion     OSCII-bot forum     Old Cockos Products Forum

All times are GMT -7. The time now is 02:12 PM.

 -- Cockos ---- REAPER 5 ---- Reaper 3 ---- Reaper 2 ---- Reaper 1 Contact Us - Çockos Incorporated - Archive - Top