Rounding numbers

  • I'd expect this code to produce 2.33 but it gives me 2.33999999999 - is this a JavaScript thing or a HISE thing? Thanks.

    Console.print(Math.round(2.337898 * 100) / 100);

  • Nope, that's standard double floating point behaviour (you don't have exact numbers but more like a guarantee how many digits are precise). But unless you are planning to simulate a scientific environment, it should not affect your functionality

    If you want 2.33, use Math.floor() instead (it still gives you the rounding error tough).

  • Ah I see, there might be another solution then - I'm using custom knob and slider graphics on my interface and I want to display the current value of those knobs in separate labels so I'm using knob.getValue(). But for things like the ADSR sustain which is in dB I don't get the nice neat -100 - 0 dB readout like the default knob provides (when set to Decibels mode). Is there a command to retrieve a knob's display value rather than its actual value?

    Edit: Actually math.floor works fine for the sustain parameter. For a pitch constant modulator though I need more precision than an integer so I used```
    Console.print(Math.floor(2.337898 * 100) / 100);

  • Engine.doubleToString(number, digits)?

  • Fantastic!

Log in to reply