1

I have the following function which totals up three field values:

function calculatePublicTransportTotalClaim(){

    var Bus_Weekly_Claim = formatNum( $('#Bus_Weekly_Claim').val() );
    var Train_Weekly_Claim = formatNum( $('#Train_Weekly_Claim').val() );
    var Tram_Weekly_Claim = formatNum( $('#Tram_Weekly_Claim').val() );

    var Total = Bus_Weekly_Claim + Train_Weekly_Claim + Tram_Weekly_Claim;

    console.log( Total )

    $('#PublicTransport_Weekly_Claim').val( Total );

}

The inputs have a default value of 0.00

And they have their values converted to ints using formatNum which looks like:

function formatNum(n) {

    var round = Math.round;

    n = round(n);

    return n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");
}

However when I call the function calculatePublicTransportTotalClaim, the returned Total is: 0.000.000.00

What have I done wrong? As the values should now be numbers, but instead they seem to be acting like strings when I'm adding them together.

3 Answers 3

4

toFixed returns a string, and adding strings together does that.

As you're replacing the period with a adding commas, you'd have to do that after the numbers are added up, so the formatNum function isn't really usable, just do this instead

function calculatePublicTransportTotalClaim(){

    var Bus_Weekly_Claim   = Math.round( $('#Bus_Weekly_Claim').val() );
    var Train_Weekly_Claim = Math.round( $('#Train_Weekly_Claim').val() );
    var Tram_Weekly_Claim  = Math.round( $('#Tram_Weekly_Claim').val() );

    var Total = Bus_Weekly_Claim + Train_Weekly_Claim + Tram_Weekly_Claim;

    var parsedTotal = Total.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,");

    $('#PublicTransport_Weekly_Claim').val( parsedTotal );

}
Sign up to request clarification or add additional context in comments.

7 Comments

The regex replace doesn't replace the period with a comma. It inserts commas around every myriad of 3 digits.
@MikeSamuel - Yes it does, didn't really read it I suppose, but same same, should be done after the numbers are added up.
No one is mentioning parseFloat(), am I being dumb?
@jacqijvv - isn't very usable either, parseFloat( '123,665,443.33' ) == 123
@MikeSamuel - I meant, yes it does what you say it does, not yes it does, as I don't agree with you, a bit ambiguous there ?
|
1

toFixed() returns a string

You are concatenating instead of adding.

Force it to return a number by adding +,
e.g. return +(n.toFixed(2).replace(/(\d)(?=(\d{3})+\.)/g, "$1,"));

Comments

1

Format the numbers after you added them together. Use parseFloat() to ensure they are being interpreted as decimal number values.

total = parseFloat(val1) + parseFloat(val2) + parseFloat(val3)

Then format the total, parseFloat() is JavaScript's equivalent to parseInt() but for decimal values

Note: you must not have commas, in the number when using parseFloat. Always add formatting only after adding values.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.