number format

Hello

I have a serious problem with number format.
I have a value like 290,955.
I need to format the number to 290,96 with 2 decimal and return a string; I use utils.numberFormat( 290.955, ‘###0.00’ ).
There are strange situations
Below the results:

=>utils.numberFormat( 290.105, ‘###0.00’ )
“290,10” ERROR

=>utils.numberFormat( 290.115, ‘###0.00’ )
“290,12” CORRECT

=>utils.numberFormat( 290.125, ‘###0.00’ )
“290,12” ERROR

=>utils.numberFormat( 290.135, ‘###0.00’ )
“290,14” CORRECT

=>utils.numberFormat( 290.145, ‘###0.00’ )
“290,14” ERROR

=>utils.numberFormat( 290.155, ‘###0.00’ )
“290,16” CORRECT

=>utils.numberFormat( 290.165, ‘###0.00’ )
“290,16” ERROR

=>utils.numberFormat( 290.175, ‘###0.00’ )
“290,18” CORRECT

=>utils.numberFormat( 290.185, ‘###0.00’ )
“290,18” ERROR

=>utils.numberFormat( 290.195, ‘###0.00’ )
“290,20” CORRECT

I have already tried with toFixed method: 290.955.toFixed(2)
Below the results:

=>290.105.toFixed( 2 )
“290.11” CORRECT

=>290.115.toFixed( 2 )
“290.12” CORRECT

=>290.125.toFixed( 2 )
“290.13” CORRECT

=>290.135.toFixed( 2 )
“290.13” ERROR

=>290.145.toFixed( 2 )
“290.14” ERROR

=>290.155.toFixed( 2 )
“290.15” ERROR

=>290.165.toFixed( 2 )
“290.17” CORRECT

=>290.175.toFixed( 2 )
“290.18” CORRECT

=>290.185.toFixed( 2 )
“290.19” CORRECT

=>290.195.toFixed( 2 )
“290.19” ERROR

How can I solve this? Is this a bug?
I use servoy 5.2.15
Thank you very much!
Roberto

Hi Roberto

I used this.

utils.numberFormat( Math.round(x*100)/100, ‘###0.00’ )

utils.numberFormat() works as you want it here (round-half-up) starting with 6.1. (SVY-135)

The native JS call Number.toFixed() works exactly as in Servoy for all (4) browsers that I tested except IE. IE will round as you expect it to. I checked the ECMA script specs. - there is nothing mentioned about what kind of rounding should be used. So .toFixed behaves according to the standard in all cases. (IE/Rhino/other browsers)

Hello

is possible to add the new fix SVY-135 in servoy 5.2.x? it’s fixed for servoy 6.1
It’s very important for our accounting!!!

Thank you very much!
Bye bye from Italy

Roberto

BW, I’m migrating my apps to Servoy 7.2, I receive this error message:

The method numberFormat() from the type JSUtils is deprecated

:shock: :shock:

Not the whole numberFormat is deprecated, but the way you use it know is deprecated.
So closely look at the (count of) arguments and if the jsdoc of thos properties are right!

Hello

no one from servoy? I need this fix on servoy 5.2.x!! :cry:

Bye
Roberto

Please post your questions/comment in the support system, there it will be seen by the Servoy Team.
Beside your question, of fixing this in Servoy 5.x, what is the reason you can’t upgrade to 6 or 7?

Thanks Harjo for reply.

I cannot upgrade to servoy 6 or 7 because now we must to finish our software. But a part of our new software is already used by many customers and we have many errors in invoices and accounting. :(

I will open a case now.

Bye
Roberto

My advice to Roberto :

Keep numerical computations like round, floor, ceiling in the numerical ‘world’…

so :
var a = 13.496
var b = ourOwnRoundFunction(a, 2) // round on 2 decimals
utils.numberformat(b, ‘###0.00’)

Regards,

Thank you for answer

Can you show me an example of “ourOwnRoundFunction”

Thank you very much!
Roberto

Hi,

You can try out this function to round your number to any precision.

function ourOwnRoundFunction(value, places) {
 
   var multiplier = Math.pow(10, places);
   return (Math.round(value * multiplier) / multiplier);
}

Then you can use this to show the number as suggested

var a = 13.496
var b = ourOwnRoundFunction(a, 2) // returns 13.5
utils.numberformat(b, '###0.00')

Thanks,

Ok, that’s the same one we use…

Thank you

I tried but… below an example:

utils.numberFormat( Math.round(290.155 * Math.pow(10, 2)) / Math.pow(10, 2), ‘###0.00’ ) = “290,15”
NOT CORRECT. The correct value is “290,16”

Thank you
Roberto

Welcome to javascript precision :

Math.round(29015.5) gives 29016
Math.round(290.155 * 100) gives 29015 !

So do the multiply with the Math.pow(10,2) into a variable beforehand for better precision…

Yeah!! Javascript precision … :D :D

We can also handle it in this way:

var multiplier = Math.pow(10, places);
return (Math.round((value * multiplier).toFixed(places)) / multiplier);

Not tested for all cases though…

Thanks,

It works with all cases… Thank you!
it’s incredible that I need to use this solution… :x

just a tip: if you are working with currency, then don’t store a floating point like “9.94” in the database but store it as an integer in cents “994”

ok but in many cases I work with floating number without store.
Example: for calc the VAT. 21% of 133,43= 26,686 and sum this value with another one similar ( with 3 or + decimals ).
I need to work with all the values but if the round of all values is incorrect, i have an error at the end of operations…

I need to use the correct utils.numberFormat function that work correctly in servoy 6 and 7 but doesn’t work in servoy 5.2.

Bye
Roberto