How to round up result

Hello,

I have small calculator script that I’m using on my client’s website.

here is the script

<script>
function LengthConverter(valNum) {
  document.getElementById("outputMeters2").innerHTML=valNum*0.09;
}
</script>
<input id="inputFeet" type="number" placeholder="How Much" oninput="LengthConverter(this.value)" onchange="LengthConverter(this.value)">
    <span id="outputMeters2">

Unfortunately when I input small number (f…e 5 or 10), as result I get long outputs (.f.e. 0.44999999999999995)

My question is how can I roundup this results?

thanks

1 Like

JS has a .round() method in the Math module.

Recall that rounding is an up or down operation. Up on 5, down on 4.

Also in the Math module we have .ceil, which method will always round up to the nearest integer.

1 Like

Thanks MTF

Can you help me, so how it will be in my example? I’m not enough strong in Js.

thx

1 Like

If we just want a smaller number (less digits), then specify the number of decimal digits in the Number.toFixed() method

0.44999999999999995.toFixed(3)    // 0.450
1 Like

MTF

I meant how do I need to implement that in my script

<script>
function LengthConverter(valNum) {
  document.getElementById("outputMeters2").innerHTML=valNum*0.09;
}
</script>

Here is my script online - http://www.lawsuitsettlementfunding.co/compare-rates/

I need to be sure, that any number don’t break my table with more digits.

thanks

1 Like
= (valNum * 0.09).toFixed(3);
2 Likes

Thank you ! :slight_smile:

2 Likes