Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding missing cents zeros #85

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 22 additions & 5 deletions jquery.priceformat.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,13 +131,9 @@

// apply cents pontuation
// This stops from adding a leading Zero '0.00' -> '.00'
if (leadingZero) {
if (leadingZero || integerVal !== "0") {
formatted = integerVal + centsSeparator + centsVal;
} else {
if (integerVal !== "0") {
formatted = integerVal + centsSeparator + centsVal;
}
else {
formatted = centsSeparator + centsVal;
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Douglas,

I've faced the same issue as you did, and tried your pull request.
It seems that this curly brace needs to be removed as well, otherwise the script will break.

Expand Down Expand Up @@ -227,6 +223,26 @@

}

// Fill cents missing
function fix_cents_by_str(str) {
if (str[str.length-3] != '.' && str[str.length-3] != ',') { // 1.11 # do nothing
if (str[str.length-2] == '.' || str[str.length-2] == ','){ // 1.1 # fill with 1 zero
str = str+"0";
} else { // 1 # fill with 2 zeroes
str = str+"00";
}
}
return str;
}
function fix_cents() {
var str = get();
var new_str = fix_cents_by_str(str);
var price = fix_cents_by_str(price_format(str));
if (new_str != price) set(price);
Copy link

@tr4nquility tr4nquility Jul 16, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this one, I think it should be if (new_str != price) set(new_str).
After applying the above changes, it works as expected.

var format = fix_cents_by_str(price_format('0', true));
if (price == format && new_str != '0' && new_str != '000' && clearOnEmpty) set('');
}

// Formatted price as a value
function price_it() {
var str = get();
Expand Down Expand Up @@ -290,6 +306,7 @@

// If value has content
if (get().length > 0) {
fix_cents();
price_it();
clear_prefix();
clear_suffix();
Expand Down