This one is more general (and faster, I think: no if-else, no regexp) Number.prototype.padRight = function (fill) { var v = this.toString().split('.'), d = v[1] || ''; return (v[0] || '0') + '.' + d + fill.substr(d.length); }
test1 = 2; test2 = 3.141592; test3 = 0.001; test4 = .33; test1.padRight('0000000000'); // returns 2.0000000000 test2.padRight('000000'); // returns 3.141592 test3.padRight('00'); // returns 0.001 test4.padRight('0000000000'); // returns 0.3300000000 As I see you're dealing with cents, money and stuff, I have to remember you that JavaScript use IEEE 754 Floating Points as its own internal number type, so 0.1 + 0.2 !== 0.3: people tend to be very picky when dealing with money, especially if it's *their* money! :) So: be careful. And if you're relying on JavaScript logic to handle people's money, change programming language, JavaScript is not good at it. On Tue, Jul 7, 2009 at 12:11, weidc<mueller.juli...@googlemail.com> wrote: > > hi, > > thats my code: > > endpreis =Math.round(endpreis*100)/100; > > to round the price of something but i'd like to have 2,00 or 2,50 > instead of 2 and 2,5. > > > i'd be happy about any help. > > --weidc -- Massimo Lombardo Linux user #437712