-1

I have a (hopefully) quick and short question. I try to find a Javascript, Jquery, or PHP function that allows me to display every number on a webpage with two decimals. Basically, 0 should be 0.00, 1.5 should be 1.50, and 1.54 should stay 1.54.

However, most functions that I found on stackoverflow, like this one, seem to require me to put in each number manually (and don't work for integers). Is there a way I can apply the function to a class? Ideally, I am looking for a function that (1) applies to all numbers that are within the same class and (2) displays each of those numbers with two decimals, even the integers.

Thank you very much in advance for your help!

Community
  • 1
  • 1
rf2012
  • 233
  • 2
  • 7
  • 13
  • 1
    "Is there a way I can apply the function to a class?" - what kind of class? Are you talking about css class? – Ja͢ck Mar 26 '13 at 10:14
  • yep, the last paragraph about classes is absolutely unclear. Can you give an example? As for the integers: just use echo sprintf('%.2f',0); – Alexey Mar 26 '13 at 10:16
  • I'm sorry that I have been unclear. I actually didn't know there were different kinds of classes. I meant a class like aoi used, so a css class I believe. – rf2012 Mar 26 '13 at 10:29

1 Answers1

0

For PHP you have number_format($whateverNumber,2) or sprintf, or even simply typecasting to float might work given the data; in javascript/jquery you can use toFixed(2);

In jquery you can do something like

$(function(){
    $(".yourClass").each(function(){
        var number = parseFloat($(this).html());
        number = isNaN(number)?0:number;
        $(this).html(number.toFixed(2));
    });
})
Bluemagica
  • 5,000
  • 12
  • 47
  • 73