search

Home  >  Q&A  >  body text

javascript - Convert a string to a number, an example is a="2,322.222", b='1,211.21', how to perform subtraction in angualr

The example is a="2,322.222",b='1,211.21', how to perform subtraction in angualr

伊谢尔伦伊谢尔伦2809 days ago557

reply all(2)I'll reply

  • 怪我咯

    怪我咯2017-05-16 13:46:20

    function(a, b) {
        a = a.replace(',', '');
        b = b.replace(',', '');
        return (+a - +b).toFixed(2);
    }
    

    reply
    0
  • 过去多啦不再A梦

    过去多啦不再A梦2017-05-16 13:46:20

    Use split(',') and press ',' to divide it into multiple arrays and judge based on the length. For example, a is 21000 and b is 11000. If there are more commas, it is judged based on the length. If the length is 3, the subscript is When 0 is 10001000, consider the Math.pow(10,n * 3) method and convert it into numbers for subtraction. You can try it

    reply
    0
  • Cancelreply