How to add a trailing zero to a price with jQuery

后端 未结 3 822
刺人心
刺人心 2020-12-01 17:49

So I have a script which returns a price for a product. However the price may or may not include trailing zeros so sometimes I might have:

258.22

and other t

3条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-01 18:14

    I don't think jQuery itself has any string padding functions (which is what you're looking for). It's trivial to do, though:

    function pad(value, width, padchar) {
    
        while (value.length < width) {
            value += padchar;
        }
        return value;
    }
    

    Edit The above is great for strings, but for your specific numeric situation, rosscj2533's answer is the better way to go.

提交回复
热议问题