Search code examples
javascriptandroidiosappery.io

Math.round (), How to with a random number?


Similar to my other issue with Auto-Calculation, when I use the mobile app I need round number's only and nothing else. In saying that, at the moment I get 1.05 instead of 1 using the code below and Math.ciel(), I really need it to round to the nearest whole number with absolute no decimal places.

var rc1FrontMM = Number(Apperyio("rc1FrontMM").val());
var rc1RearMM = Number(Apperyio("rc1RearMM").val());
var rc1TyreRatio1 = Number(Apperyio("rc1TyreRatio1").val());
var rc1TyreRatio2 = Number(Apperyio("rc1TyreRatio2").val());

var rc1TyreRatio1 = (rc1RearMM / rc1FrontMM).Math.ceil(num * 100)/100;
var rc1TyreRatio2 = (rc1RearMM / rc1FrontMM).Math.ceil(num * 100)/100;

Apperyio('rc1TyreRatio1').val(rc1TyreRatio1.Math.ceil(num * 100)/100);
Apperyio('rc1TyreRatio2').val(rc1TyreRatio2.Math.ceil(num * 100)/100);

console.log(rc1TyreRatio1 = (rc1RearMM / rc1FrontMM).Math.ceil(num * 100)/100);
console.log(rc1TyreRatio2 = (rc1RearMM / rc1FrontMM).Math.ceil(num * 100)/100);

So far, it seems to work beautifully except I still get decimal's without rounding to the nearest whole number.

Advice?

Edit: 3:20pm 17/6/16 AEST Using Javascript only due to limitations of Appery.io platform (as explained prior, this wasn't my first choice, it was the client's choice).


Solution

  • Why don't you use the toFixed(x) method? It'll round down or up to the nearest integer if you use x = 0.