Merge pull request #955 from anandpdoshi/anand-dec-17

[fix] ability to parse number format # ###,## with appropriate rounding
This commit is contained in:
Anand Doshi 2014-12-18 17:32:10 +05:30
commit 0974c87f85
2 changed files with 32 additions and 15 deletions

View file

@ -387,29 +387,44 @@ frappe.ui.form.ControlInt = frappe.ui.form.ControlData.extend({
frappe.ui.form.ControlFloat = frappe.ui.form.ControlInt.extend({
parse: function(value) {
return isNaN(parseFloat(value)) ? null : flt(value);
return isNaN(parseFloat(value)) ? null : flt(value, this.get_precision());
},
format_for_input: function(value) {
var number_format;
var precision = this.df.precision || cint(frappe.boot.sysdefaults.float_precision, null);
if (this.df.fieldtype==="Float" && this.df.options && this.df.options.trim()) {
number_format = get_number_format(this.get_currency());
number_format = this.get_number_format();
}
var formatted_value = format_number(parseFloat(value), number_format, precision);
var formatted_value = format_number(parseFloat(value), number_format, this.get_precision());
return isNaN(parseFloat(value)) ? "" : formatted_value;
},
// even a float field can be formatted based on currency format instead of float format
get_currency: function() {
return frappe.meta.get_field_currency(this.df, this.get_doc());
get_number_format: function() {
var currency = frappe.meta.get_field_currency(this.df, this.get_doc());
return get_number_format(currency);
},
get_precision: function() {
// round based on field precision or float precision, else don't round
return this.df.precision || cint(frappe.boot.sysdefaults.float_precision, null);
}
});
frappe.ui.form.ControlCurrency = frappe.ui.form.ControlFloat.extend({
format_for_input: function(value) {
var formatted_value = format_number(parseFloat(value),
get_number_format(this.get_currency()), this.df.precision || null);
var formatted_value = format_number(parseFloat(value), this.get_number_format(), this.get_precision());
return isNaN(parseFloat(value)) ? "" : formatted_value;
},
get_precision: function() {
// always round based on field precision or currency's precision
// this method is also called in this.parse()
if (!this.df.precision) {
this.df.precision = get_number_format_info(this.get_number_format()).precision;
}
return this.df.precision;
}
});

View file

@ -11,7 +11,8 @@ function flt(v, decimals, number_format) {
// strip currency symbol if exists
if(v.indexOf(" ")!=-1) {
v = v.split(" ")[1];
// using slice(1).join(" ") because space could also be a group separator
v = isNaN(parseFloat(v.split(" ")[0])) ? v.split(" ").slice(1).join(" ") : v;
}
v = strip_number_groups(v, number_format);
@ -40,15 +41,16 @@ function cint(v, def) {
function strip_number_groups(v, number_format) {
if(!number_format) number_format = get_number_format();
var info = get_number_format_info(number_format);
// strip groups (,)
if(get_number_format_info(number_format).group_sep==".") {
v = v.replace(/\./g,'');
var group_regex = new RegExp(info.group_sep==="." ? "\\." : info.group_sep, "g");
v = v.replace(group_regex, "");
// sanitize decimal separator to .
v = v.replace(/,/g, ".");
} else {
v=v.replace(/,/g,'');
// replace decimal separator with (.)
if (info.decimal_str!=="." && info.decimal_str!=="") {
var decimal_regex = new RegExp(info.decimal_str, "g");
v = v.replace(decimal_regex, ".");
}
return v;