Search code examples
jqueryeuro

Price calculation, comma, and conditional statement


All sorts of topics are dedicated to the European comma problem, i.e. How to change the ',' to an '.', But I've not found a solution for mine yet.

I want users to be able to input in the comma-value manner (e.g. 3,99), Then calculate some stuff and output some of it again in comma's. The if-statement should mention that if the price at the end (earnings) turns negative, it turns red (i.e. NOT possible).

I am able to change the point to a comma and output that, but the other way around I'm dazzled. Here's my code thus far:

I've managed to get the whole thing working based on @Zirak's comments and a little bit of me-magic, see code below!

function dollarformat(num) {
    num = num.toString().replace(/\u20ac/g, 'Euro');
    if(isNaN(num)) num = "0";
        cents = Math.floor((num*100+0.5)%100);
        num = Math.floor((num*100+0.5)/100).toString();
    if(cents < 10) cents = "0" + cents;
        for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
            num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
    return (num + ',' + cents);}

$('#prijsnow').keyup(function(num) {

        var comma = $(this).val().replace(',', '.');
        $('#vat').text(dollarformat(comma / 119 * 100));
        $('#earnings').text(dollarformat(comma / 119 * 25));
                // 0.80 is the minimum amount, I've put it in hardcode (could be dynamic as well)
        if (comma < 0.80) { $('#earnings').text(0); } else {$('#earnings').text(dollarformat(comma / 119 * 75 - 0.50))};

                // Nice fix for the colour problem, easy CSS attribution
        if (comma < 0.80) { $('#earnings').css("color","red"); } 
        if (comma > 0.80) { $('#earnings').css("color","green"); }

    });

Solution

  • As described above, a possible way to fix this problem is:

    function dollarformat(num) {
        num = num.toString().replace(/\u20ac/g, 'Euro');
        if(isNaN(num)) num = "0";
            cents = Math.floor((num*100+0.5)%100);
            num = Math.floor((num*100+0.5)/100).toString();
        if(cents < 10) cents = "0" + cents;
            for (var i = 0; i < Math.floor((num.length-(1+i))/3); i++)
                num = num.substring(0,num.length-(4*i+3))+','+num.substring(num.length-(4*i+3));
        return (num + ',' + cents);}
    
    $('#prijsnow').keyup(function(num) {
    
            var comma = $(this).val().replace(',', '.');
            $('#vat').text(dollarformat(comma / 119 * 100));
            $('#earnings').text(dollarformat(comma / 119 * 25));
                    // 0.80 is the minimum amount, I've put it in hardcode (could be dynamic as well)
            if (comma < 0.80) { $('#earnings').text(0); } else {$('#earnings').text(dollarformat(comma / 119 * 75 - 0.50))};
    
                    // Nice fix for the colour problem, easy CSS attribution
            if (comma < 0.80) { $('#earnings').css("color","red"); } 
            if (comma > 0.80) { $('#earnings').css("color","green"); }
    
        });