最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Adding two floats in javascript - Stack Overflow

programmeradmin1浏览0评论

The js code is

var receivedamt = parseFloat($('#cashRecText').val()).toFixed(2); 
console.log(receivedamt);
var addon = parseFloat('5.00').toFixed(2);
console.log(addon);
addon = parseFloat(receivedamt).toFixed(2)+parseFloat(addon).toFixed(2);
console.log(addon);

But the output is

3.00
5.00
3.005.00

How will get sum as

8.00

The js code is

var receivedamt = parseFloat($('#cashRecText').val()).toFixed(2); 
console.log(receivedamt);
var addon = parseFloat('5.00').toFixed(2);
console.log(addon);
addon = parseFloat(receivedamt).toFixed(2)+parseFloat(addon).toFixed(2);
console.log(addon);

But the output is

3.00
5.00
3.005.00

How will get sum as

8.00
Share Improve this question edited Jul 6, 2016 at 9:49 humbleiam asked Jul 6, 2016 at 9:47 humbleiamhumbleiam 1,0092 gold badges14 silver badges34 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 9
var addon = (parseFloat(receivedamt)+parseFloat(addon)).toFixed(2);

EDIT: The reason that the original code doesn't work, is that the toFixed function returns a string. Therefore, two strings were "added" rather than two float values.

Try this :

addon = parseFloat(receivedamt)+parseFloat(addon);

function sumFloatNumbers(x, y) {
    const arr1 = x.toString().split('');
    const arr2 = y.toString().split('');

    let dotPos1 = arr1.indexOf('.');
    if(dotPos1 < 0){
        arr1.push(".");
        arr1.push("0");
        dotPos1 = arr1.indexOf('.');
    }
    let dotPos2 = arr2.indexOf('.');
    if(dotPos2 < 0){
        arr2.push(".");
        arr2.push("0");
        dotPos2 = arr2.indexOf('.');
    }
    const dotPos = Math.max(dotPos1, dotPos2);
    let diff1 = dotPos - dotPos1;
    let diff2 = dotPos - dotPos2;
    if (diff1 > 0) {
        for (let i = 0; i < diff1; i++) {
            arr1.unshift("0");
        }
    }
    if (diff2 > 0) {
        for (let i = 0; i < diff2; i++) {
            arr2.unshift("0");
        }
    }

    const len1 = arr1.length;
    const len2 = arr2.length;
    const len = Math.max(len1, len2);
    diff1 = len - len1;
    diff2 = len - len2;
    if (diff1 > 0) {
        for (let i = 0; i < diff1; i++) {
            arr1.push("0");
        }
    }
    if (diff2 > 0) {
        for (let i = 0; i < diff2; i++) {
            arr2.push("0");
        }
    }

    const arr = [];
    let sum = 0,
        val1 = 0,
        val2 = 0;
    for (let i = len - 1; i >= 0; i--) {
        if (arr1[i] == '.') {
            arr.unshift('.');
            continue;
        }
        sum = parseInt(arr1[i]) + parseInt(arr2[i]) + val1;
        val1 = sum >= 10 ? 1 : 0;
        val2 = sum >= 10 ? sum - 10 : sum;
        arr.unshift(val2.toString());
    }
    if (val1 == 1) {
        arr.unshift('1');
    }
    return parseFloat(arr.join(''));
}

const num1 = 7.3333333;
const num2 = 4.44444444444;
console.log(sumFloatNumbers(num1, num2));

发布评论

评论列表(0)

  1. 暂无评论