Convert Fraction String to Decimal?

前端 未结 16 623
谎友^
谎友^ 2020-12-05 13:55

I\'m trying to create a javascript function that can take a fraction input string such as \'3/2\' and convert it to decimal—either as a string \'1.5\'

相关标签:
16条回答
  • 2020-12-05 14:40

    const fractionStringToNumber = s => s.split("/").map(s => Number(s)).reduce((a, b) => a / b);
    
    console.log(fractionStringToNumber("1/2"));
    console.log(fractionStringToNumber("1/3"));
    console.log(fractionStringToNumber("3/2"));
    console.log(fractionStringToNumber("3/1"));
    console.log(fractionStringToNumber("22/7"));
    console.log(fractionStringToNumber("355 / 113"));
    console.log(fractionStringToNumber("8/4/2"));
    
    console.log(fractionStringToNumber("3")); // => 3, not "3"

    0 讨论(0)
  • 2020-12-05 14:41

    Here is the bare bones minimal code needed to do this:

    var a = "3/2";
    var split = a.split('/');
    var result = parseInt(split[0], 10) / parseInt(split[1], 10);
    alert(result); // alerts 1.5
    

    JsFiddle: http://jsfiddle.net/XS4VE/

    Things to consider:

    • division by zero
    • if the user gives you an integer instead of a fraction, or any other invalid input
    • rounding issues (like 1/3 for example)
    0 讨论(0)
  • 2020-12-05 14:42

    Something like this:

    bits = fraction.split("/");
    return parseInt(bits[0],10)/parseInt(bits[1],10);
    
    0 讨论(0)
  • 2020-12-05 14:43

    It works with eval() method but you can use parseFloat method. I think it is better! Unfortunately it will work only with that kind of values - "12.2" not with "5/8", but since you can handle with calculation I think this is good approach!

    0 讨论(0)
  • 2020-12-05 14:45

    Function (ES6):

    function fractionToDecimal(fraction) {
      return fraction
        .split('/')
        .reduce((numerator, denominator, i) =>
          numerator / (i ? denominator : 1)
        );
    }
    

    Function (ES6, condensed):

    function fractionToDecimal(f) {
      return f.split('/').reduce((n, d, i) => n / (i ? d : 1));
    }
    

    Examples:

    fractionToDecimal('1/2');     // 0.5
    fractionToDecimal('5/2');     // 2.5
    fractionToDecimal('1/2/2');   // 0.25
    fractionToDecimal('10/5/10'); // 0.2
    fractionToDecimal('0/1');     // 0
    fractionToDecimal('1/0');     // Infinity
    fractionToDecimal('cat/dog'); // NaN
    
    0 讨论(0)
  • 2020-12-05 14:46

    From a readability, step through debugging perspective, this may be easier to follow:

    // i.e. '1/2' -> .5
    // Invalid input returns 0 so impact on upstream callers are less likely to be impacted
    function fractionToNumber(fraction = '') {
        const fractionParts = fraction.split('/');
        const numerator = fractionParts[0] || '0';
        const denominator = fractionParts[1] || '1';
        const radix = 10;
        const number = parseInt(numerator, radix) / parseInt(denominator, radix);
        const result = number || 0;
    
        return result;
    }
    
    0 讨论(0)
提交回复
热议问题