Can I move a decimal to javascript without math?

I would like to be able to move the decimal point 2 places over an unknown number of numbers without using math. I know this seems odd, but the ultimate accuracy causes some shifts. My javascript is not strong, but I would really like to know how to chop a number and do it if possible. Thus, I hope that great people will help you.

Problem:

  • 575/960 = 0.5989583333333334 using the console
  • I would like to make a copy and passive percentage: 59.8958333333333434%
  • If I use math and multiply it by 100, it returns 59.895833333333336 because of the finite precision

Is there a way to make this line and just move the decimal 2 places to the right to skip the math?

Here is also a fiddle with codes: http://jsfiddle.net/dandenney/W9fXz/

If you want to know why I need this and want accuracy, this is for this small tool that I did to get percentage answers without using a calculator: http://responsv.com/flexible-math

+3
source share
3 answers

If the original number refers to this type of known structure and always has at least two digits to the right of the decimal, you can do this as follows:

function makePercentStr(num) {
    var numStr = num + "";
    // if no decimal point, add .00 on end
    if (numStr.indexOf(".") == -1) {
        numStr += ".00";
    } else {
        // make sure there at least two chars after decimal point
        while (!numStr.match(/\.../)) {
            numStr += "0";        
        }
    }
    return(numStr.replace(/\.(..)/, "$1.")
           .replace(/^0+/, "")    // trim leading zeroes
           .replace(/\.$/, "")    // trim trailing decimals
           .replace(/^$/, "0")    // if empty, add back a single 0
           + "%");
}

Working demo with test cases: http://jsfiddle.net/jfriend00/ZRNuw/

+4
source

The question requires solving the problem without Math, but the solution below includes math. I leave it for reference only

function convertToPercentage(num) {
    //Changes the answer to string for checking
    //the number of decimal places.
    var numString = num + '';
    var length = (numString).substring(numString.indexOf(".")+1).length;

    //if the original decimal places is less then
    //no need to display decimals as we are multiplying by 100
    //else remove two decimals from the result
    var precision = (length < 2 ? 0 : length-2);

    //if the number never contained a decimal. 
    //Don't display decimal.
    if(numString.indexOf(".") === -1) {
         precision = 0;   
    }        
    return (num * 100).toFixed(precision) + "%";
}        

jsFiddle , .

+1

I used this method because of the risk of floating errors:

const DECIMAL_SEP = '.';

function toPercent(num) {
  const [integer, decimal] = String(num).split(DECIMAL_SEP);

  // no decimal, just multiply by 100
  if(typeof decimal === 'undefined') {
    return num * 100;
  }

  const length = decimal.length;

  if(length === 1) {
    return Number(integer + decimal + '0');
  }

  if(length === 2) {
    return Number(integer + decimal);
  }

  // more than 2 decimals, we shift the decimal separator by 2
  return Number(integer + decimal.substr(0, 2) + DECIMAL_SEP + decimal.substr(2));
}

console.log(toPercent(10));
console.log(toPercent(1));
console.log(toPercent(0));
console.log(toPercent(0.01));
console.log(toPercent(0.1));
console.log(toPercent(0.12));
console.log(toPercent(0.123));
console.log(toPercent(12.3456));
Run codeHide result
0
source

All Articles