tags:

views:

36

answers:

2

I need to parseFloat this then to replace the dot with a comma:

sum="<?php echo substr($_tarif_membre,0,-2).'.'.substr($_tarif_membre,-2,2) ?>" *  $('#tarif_membre').val()
total_total= sum.replace('.',',')
$('#total_total').val(sum)

if i dont multiply the php echoed variable by $('#tarif_membre').val() it will still show the result as a float:12.00 (as it is echoed from php) but if i multiply by $('#tarif_membre').val() the result will show 12, why? and why does the replace method does not work(i need to show comma separated values)

A: 

In Javascript, there's no difference between an integer and a float. var x = 12.00 will, when converted back to string, just show "12".

If you want to show the number formatted, you either have to keep it as a string ("12.00"); or you need some code similar in behavior to printf (which Javascript doesn't have; but there are Javascript libraries available that implement it, for example here) to format it again with as many decimals as you like.

bart
A: 

I think this is what you're trying to do:

var sum = 12.00 * parseFloat($('#tarif_membre').val());
total_total= sum.toFixed(2).replace('.',',');
$('#total_total').val(total_total);

You can try it out here.

You should leave off the quotes since it's a number, then parseFloat() the #tarif_membre .val(), since that will be a string, then they key to mainain decimal places for money here is calling .toFixed() on that float, this will get you "12.00", a string. You can them replace the . for a , (for international formatting I'm guessing?) Then I'm sticking that result into the #total_total, though this piece I'm unsure on.

For the other question, why doesn't .replace() work? It's because in your code it was a float, not a string, and only strings have the replace method.

Nick Craver
Thank you man!, this is exactly what im trying to achieve.
tada