[FIX] completely broken float formatting when leading 0

Formatting converted the decimal part into an integer, dropping all
leading 0s and failing to reinsert them.

The conversion also lead to compounded errors, casting 0.1100 into
0.10999 (something like that).

Replace this by a straight float formatting via _.sprintf and the
slicing of the decimal part from the resulting string (Math.abs and %1
clamp the value to format to the [0, 1[ range, ensuring we just need
to slice off the first two characters). Also add some tests.

lp bug: https://launchpad.net/bugs/879355 fixed

bzr revid: xmo@openerp.com-20111021120741-813im5bzup7vho1f
This commit is contained in:
Xavier Morel
2011-10-21 14:07:41 +02:00
parent 4da3e5d354
commit 01897c8aa6
3 changed files with 19 additions and 7 deletions
+7 -4
View File
@@ -28,10 +28,13 @@ openerp.web.format_value = function (value, descriptor, value_if_empty) {
case 'float':
var precision = descriptor.digits ? descriptor.digits[1] : 2;
var int_part = Math.floor(value);
var dec_part = Math.abs(Math.floor((value % 1) * Math.pow(10, precision)));
return _.sprintf('%d%s%d',
int_part,
openerp.web._t.database.parameters.decimal_point, dec_part);
var dec_part = _.sprintf(
'%.' + precision + 'f',
Math.abs(value) % 1).substring(2);
return _.sprintf('%d%s%s',
int_part,
openerp.web._t.database.parameters.decimal_point,
dec_part);
case 'float_time':
return _.sprintf("%02d:%02d",
Math.floor(value),
+12
View File
@@ -27,6 +27,18 @@ $(document).ready(function () {
var fl = 12.1234;
var str = openerp.web.format_value(fl, {type:"float"});
equal(str, "12.12");
equal(openerp.web.format_value(12.02, {type: 'float'}),
'12.02');
equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 3]}),
'0.000');
equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 4]}),
'0.0002');
equal(openerp.web.format_value(0.0002, {type: 'float', digits: [1, 6]}),
'0.000200');
equal(openerp.web.format_value(1, {type: 'float', digits: [1, 6]}),
'1.000000');
equal(openerp.web.format_value(1, {type: 'float'}),
'1.00');
});
test("parse_datetime", function () {
var val = openerp.web.str_to_datetime("2009-05-04 12:34:23");
-3
View File
@@ -33,9 +33,6 @@
<script src="/web/static/src/js/search.js"></script>
<script src="/web/static/src/js/view_form.js"></script>
<script src="/web/static/src/js/view_list.js"></script>
<script type="text/javascript">
QWeb2.add_template('/web/static/src/xml/web.xml');
</script>
</head>
<body id="oe" class="openerp">
<h1 id="qunit-header">OpenERP web Test Suite</h1>