jquery - Price calculation, comma, and conditional statement -
all sorts of topics dedicated european comma problem, i.e. how change ',' '.', i've not found solution mine yet.
i want users able input in comma-value manner (e.g. 3,99), calculate stuff , output of again in comma's. if-statement should mention if price @ end (earnings) turns negative, turns red (i.e. not possible).
i able change point comma , output that, other way around i'm dazzled. here's code far:
i've managed whole thing working based on @zirak's comments , 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; (var = 0; < 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 minimum amount, i've put in hardcode (could dynamic well) if (comma < 0.80) { $('#earnings').text(0); } else {$('#earnings').text(dollarformat(comma / 119 * 75 - 0.50))}; // nice fix colour problem, easy css attribution if (comma < 0.80) { $('#earnings').css("color","red"); } if (comma > 0.80) { $('#earnings').css("color","green"); } });
just looking @ script shouldn't have problem.
if (/\./.test(num)) num.replace('.', ','); //or if want other way around if (/\,/.test(num)) num.replace(',', '.');
for negative price, can use class:
if( $(earnings).val() < 0 ) $(earnings).addclass('negativeval');
and in css:
.negativeval { color : red; }
also, save trouble: did try splitting number using split?
Comments
Post a Comment