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 *开发者_如何学JAVA 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"); }
});
Just looking at the script you shouldn't have a problem.
if (/\./.test(num))
num.replace('.', ',');
//Or if you want the other way around
if (/\,/.test(num))
num.replace(',', '.');
For the negative price, you can use a class:
if( $(earnings).val() < 0 )
$(earnings).addClass('negativeVal');
And in your CSS:
.negativeVal {
color : red;
}
Also, to save you trouble: Did you try splitting the number using split?
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"); }
});
精彩评论