fixes in format number

This commit is contained in:
Anand Doshi 2013-03-13 19:15:35 +05:30
parent 32d8e5e6c7
commit 8912db7ec1
3 changed files with 52 additions and 35 deletions

View file

@ -95,7 +95,7 @@ function nth(number) {
return number+s;
}
function flt(v, decimals) {
function flt(v, decimals, number_format) {
if(v==null || v=='')return 0;
if(typeof v!=="number") {
@ -106,16 +106,7 @@ function flt(v, decimals) {
v = v.split(" ")[1];
}
// strip groups (,)
if(get_number_format_info(get_number_format()).group_sep==".") {
v = v.replace(/\./g,'');
// sanitize decimal separator to .
v = v.replace(/,/g, ".");
} else {
v=v.replace(/,/g,'');
}
v = strip_number_groups(v, number_format);
v=parseFloat(v);
if(isNaN(v))
@ -127,6 +118,22 @@ function flt(v, decimals) {
return v;
}
function strip_number_groups(v, number_format) {
if(!number_format) number_format = get_number_format();
// strip groups (,)
if(get_number_format_info(number_format).group_sep==".") {
v = v.replace(/\./g,'');
// sanitize decimal separator to .
v = v.replace(/,/g, ".");
} else {
v=v.replace(/,/g,'');
}
return v;
}
function esc_quotes(s) {
if(s==null)s='';
return s.replace(/'/, "\'");

View file

@ -878,13 +878,19 @@ IntField.prototype.format_input = function() {
function FloatField() { } FloatField.prototype = new DataField();
FloatField.prototype.validate = function(v) {
var v= parseFloat(v);
if(isNaN(v))
if(isNaN(parseFloat(v)))
return null;
else
v = flt(v);
return v;
};
FloatField.prototype.format_input = function() {
if(this.input.value==null) this.input.value='';
else {
var format;
if(this.get_field_currency) format = get_number_format(this.get_field_currency());
this.input.value = format_number(parseFloat(this.input.value), format);
}
}
FloatField.prototype.onmake_input = function() {
if(!this.input) return;
@ -906,18 +912,25 @@ function CurrencyField() { } CurrencyField.prototype = new FloatField();
CurrencyField.prototype.validate = function(v) {
if(v==null || v=='')
return 0;
return flt(v);
return flt(v, null, get_number_format(this.get_field_currency()));
}
CurrencyField.prototype.get_formatted = function(val) {
if(this.not_in_form)
return val;
CurrencyField.prototype.get_field_currency = function() {
var doc = null;
if(this.doctype && this.docname && locals[this.doctype])
doc = locals[this.doctype][this.docname];
return get_currency_symbol(wn.meta.get_field_currency(this.df, doc))
+ " " + format_number(val);
return wn.meta.get_field_currency(this.df, doc);
};
CurrencyField.prototype.get_formatted = function(val) {
if(this.not_in_form)
return val;
return format_currency(val, this.get_field_currency());
}
CurrencyField.prototype.set_disp = function(val) {
CurrencyField.prototype.set_disp = function(val) {
this.set_disp_html(this.get_formatted(val));
}

View file

@ -17,26 +17,17 @@ window.format_number = function(v, format, decimals){
}
info = get_number_format_info(format);
if(isNaN(+v) || v==null) {
v=0;
};
// remove group separators (if any)
if(typeof(v)==="string") {
v = replace_all(v, info.group_sep, "");
}
v = flt(v, null, format);
if(v<0) var is_negative = true;
v = Math.abs(v);
//Fix the decimal first, toFixed will auto fill trailing zero.
decimals = decimals || info.precision;
v = v.toFixed(decimals);
var part = v.split('.');
// get group position and parts
var group_position = info.group_sep ? 3 : 0;
@ -72,9 +63,8 @@ window.format_number = function(v, format, decimals){
};
function format_currency(v, currency) {
var format = wn.model.get_value("Currency", currency,
"number_format") || get_number_format();
var format = get_number_format(currency);
var symbol = get_currency_symbol(currency);
if(symbol)
@ -94,13 +84,20 @@ function get_currency_symbol(currency) {
}
var global_number_format = null;
function get_number_format() {
function get_number_format(currency) {
if(!global_number_format) {
global_number_format = wn.boot.sysdefaults.number_format
|| wn.model.get_value("Currency", wn.boot.sysdefaults.currency, "number_format")
|| "#,###.##";
}
return global_number_format;
var number_format;
if(currency) {
number_format = wn.model.get_value("Currency", currency,
"number_format");
}
return number_format || global_number_format;
}
function get_number_format_info(format) {