Skip to content

Commit 6580437

Browse files
author
Diego Plentz
committed
adding fix when the user doesnt set the full precision of the number before trying to format it
1 parent f4d64ec commit 6580437

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

src/jquery.maskMoney.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -169,12 +169,12 @@
169169

170170
if(splitedNumbers.indexOf(".") > -1) {
171171
// grab just the last part of the splited numbers
172-
decimalPart = splitedNumbers.slice(splitedNumbers.indexOf("."));
172+
decimalPart = splitedNumbers.slice(splitedNumbers.indexOf(".") + 1);
173173
// grab just the right ammount of numbers that we need for the precision
174174
decimalPart = decimalPart.slice(-settings.precision);
175175
}
176176
if (decimalPart.length < settings.precision) {
177-
leadingZeros = new Array(settings.precision+1).join(0);
177+
leadingZeros = new Array(settings.precision+1-decimalPart.length).join(0);
178178
decimalPart += leadingZeros;
179179
splitedNumbers += leadingZeros;
180180
}

test/mask_test.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,13 @@ test("with a pre-formatted number", function() {
2222
equal(input.val(), "123.45", "keeps the number precision");
2323
});
2424

25+
test("with a pre-formatted number smaller then the set precision", function() {
26+
var input = $("#input1").maskMoney();
27+
input.val("123,4");
28+
input.maskMoney("mask");
29+
equal(input.val(), "123.40", "keeps the number precision");
30+
});
31+
2532
test("with negative symbol and a field that doesn't allow negative ", function() {
2633
var input = $("#input1").maskMoney({allowNegative: false});
2734
input.val("-123");

0 commit comments

Comments
 (0)