limiting a float

elad's icon

hello,

can anyone tell me how do I limit the numbers after the point in a float? I'm trying to use toFixed like in JavaScript, but id doesnt seem to work.

Also, is there a documentation of the Javascript in Max, and actually how come some function available in regular Javascript aren't in Max, I'm a bit confused as to how this thing works...

Thanks!

Beep's icon

you can set limits to a number box using the inspector, just click on the object, press cmd i, and you can set the maximum and minimum values there. You could also use the [minimum] and [maximum] objects to do limiting. Is that what you were after?

Stephane Morisse's icon

I think this is more a question about decimal places...

Beep's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Okay, I see what your getting at. How about this:

This limits the example to three and two decimal places respectively

mark2e's icon
Max Patch
Copy patch and select New From Clipboard in Max.

Or use [sprintf]

elad's icon

yes, i was talking about decimal places. I copy the text and press new from clipboard, but it just shows me the text. I am using Max 4.5...
Isn't there a way to do this using code? you know? just a function?

thanks!!

elad's icon

like in regular JavaScript I will do:

(0.595).toFixed(2) === '0.59'

Peter Castine's icon

The problem with using toFixed() in your JavaScript is that if it tries to send out 0.59 (for instance) through an outlet, what comes out is a float. The value 0.59 does not exist in binary floating point. The JavaScript Number class does a lot of funky work to present you with values like 0.59, but all that goes out the door when you send them through a Max outlet, where they become plain-vanilla IEEE floating point values.

There are multiple threads on the board about the idiosyncrasies of binary floating point arithmetic. You need to understand this stuff or Max will bite you where it hurts.

See, for instance, in particular the linked article by Peter Elsea.

Dave Mollen's icon

Or [round 0.1].

elad's icon

Nicholas, thank you very much, this is exactly what I need. Only that I really want this as a code I can use in JS. Is there a way to use this function - sprintf - as a command in the JS?

It will just simplify the amount of patchers I will need to use If I'll be able to this inside the code....
Thank you everybody else too.