Forums > Javascript

limiting a float

August 14, 2012 | 6:59 pm

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!


August 14, 2012 | 7:50 pm

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?


August 14, 2012 | 9:49 pm

I think this is more a question about decimal places…


August 14, 2012 | 10:50 pm

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

– Pasted Max Patch, click to expand. –

This limits the example to three and two decimal places respectively


August 15, 2012 | 12:03 am

Or use [sprintf]

– Pasted Max Patch, click to expand. –

August 15, 2012 | 10:14 am

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!!


August 15, 2012 | 11:47 am

like in regular JavaScript I will do:

(0.595).toFixed(2) === ’0.59′


August 15, 2012 | 1:20 pm

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, < http://cycling74.com/forums/topic.php?id=24639> in particular the linked article by Peter Elsea.


August 15, 2012 | 2:37 pm

Or [round 0.1].


August 15, 2012 | 6:02 pm

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.


Viewing 10 posts - 1 through 10 (of 10 total)