# Utils.numberFormat and toFixed

**URL:** <https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578>\
**Category:** Classic Servoy\
**Created:** [October 12, 2006, 12:18pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578 "2006-10-12T12:18:03Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cbarbal](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@cbarbal](https://forum.servoy.com/u/cbarbal)\
**Post date:** [October 12, 2006, 12:18pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578/1 "2006-10-12T12:18:03Z")

</div>

I have two formulas that work correctly, one calculates the discount and the other the total.

> dto\_importe
> 
> if ( dto \>0 )  
> {  
> var importe = importe\_bruto \* ( dto / 100);  
> var total = importe.toFixed(2);  
> //var total = utils.numberFormat(importe, 2);
> 
> return total;  
> }  
> else  
> {  
> return null  
> }
> 
> total\_linea
> 
> if ( importe\_bruto\>0 )  
> {  
> var total = importe\_bruto - dto\_importe
> 
> return total;  
> }  
> else  
> {  
> return null  
> }

But change in "dto\_importe " var total = importe.toFixed(2) by var total = utils.numberFormat(importe, 2), does not work the formula “total\_linea”.

Because?

Carles Barbal

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [October 12, 2006, 12:46pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578/2 "2006-10-12T12:46:44Z")

</div>

Hi Carles,

I am surprised that .toFixed() works at all for you.  
Both .toFixed() and utils.numberFormat() return a string.  
And in your ‘total\_linea’ calculation you try to subtract with a string value. It won’t work.  
Make sure you return a number in your ‘dto\_importe’ calculation by using the utils.stringToNumber() function like so: ```  
return utils.stringToNumber(total);

```auto
This way your 'total_linea; calculation will be calculating with a correct value type.

Hope this helps.
```

---

<div class="post-metadata">

**Author:** ![cbarbal](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@cbarbal](https://forum.servoy.com/u/cbarbal)\
**Post date:** [October 12, 2006, 3:10pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578/3 "2006-10-12T15:10:41Z")

</div>

Hi Robert,

A explanation. They are numeric fields calculated and stored. It represents that they store like string instead of numbers in database?.

Utils.numberFormat() it works like Round() of FileMaker not like .toFixed(), that according to which number does not round well.

I sorry my English is very bad .

Carles Barbal

---

<div class="post-metadata">

**Author:** ![ROCLASI](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/roclasi/32/4371_2.png) [@ROCLASI](https://forum.servoy.com/u/ROCLASI)\
**Post date:** [October 12, 2006, 6:12pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578/4 "2006-10-12T18:12:01Z")

</div>

Hi Carles,

> cbarbal:  
> A explanation. They are numeric fields calculated and stored. It represents that they store like string instead of numbers in database?.

That is correct. Those functions return string values.

> cbarbal:  
> Utils.numberFormat() it works like Round() of FileMaker not like .toFixed(), that according to which number does not round well.

Please take a look at the following threads:  
[http://forum.servoy.com/viewtopic.php?t=4910](http://forum.servoy.com/viewtopic.php?t=4910)  
[http://forum.servoy.com/viewtopic.php?t=5919](http://forum.servoy.com/viewtopic.php?t=5919)  
[http://forum.servoy.com/viewtopic.php?t=5919](http://forum.servoy.com/viewtopic.php?t=5919)

Hope this helps.

---

<div class="post-metadata">

**Author:** ![cbarbal](https://avatars.discourse-cdn.com/v4/letter/c/bb73d2/32.png) [@cbarbal](https://forum.servoy.com/u/cbarbal)\
**Post date:** [October 13, 2006, 8:30pm UTC](https://forum.servoy.com/t/utils-numberformat-and-tofixed/6578/5 "2006-10-13T20:30:25Z")

</div>

Hi Robert,

This functions work like Round of FileMaker.

```auto
utils.stringToNumber(utils.numberFormat(total, 2)) /100

```

Thanks for your aid.

Carles Barbal
