# Servoy 5: Decimal calculation issue

**URL:** <https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495>\
**Category:** Classic Servoy\
**Created:** [December 9, 2009, 11:16am UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495 "2009-12-09T11:16:50Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [December 9, 2009, 11:16am UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/1 "2009-12-09T11:16:50Z")

</div>

Hi everybody,

We have in our solution the following calculation:

```auto
function cal_total_line_cost()
{
return (fld_quantity*fld_unit_cost)-(fld_discount*fld_quantity*fld_unit_cost);
}

```

The result is stored in the database.  
Here are the datatypes:  
cal\_total\_line\_cost → numeric(19.2)  
fld\_quantity → integer  
fld\_unit\_cost → numeric(19.2)  
fld\_discount → numeric(19.3)

In Servoy 4 the result given was correct. But now on Servoy 5 it seems to round the result from the first two digits of the number.  
Here are few examples:  
fld\_quantity = 1  
fld\_unit\_cost = 192.48  
fld\_discount = 0.000  
**cal\_total\_line\_cost = 190.00** (which is incorrect, should be 192.48)

fld\_quantity = 2  
fld\_unit\_cost = 2.51  
fld\_discount = 0.000  
**cal\_total\_line\_cost = 5.00** (which is incorrect, should be 5.02)

fld\_quantity = 5  
fld\_unit\_cost = 1.64  
fld\_discount = 0.350  
**cal\_total\_line\_cost = 5.30** (which is incorrect, should be 5.33)

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [December 9, 2009, 12:51pm UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/2 "2009-12-09T12:51:12Z")

</div>

After several test we found the source of the problem.

In fact, for some reason, if you save the value returned by a calculation into a field of datatype numeric(x,y), y will be the number of digits before and after the ‘,’  
For example, 23.56 in numeric(5,2) will save 24.00 (rounded up)  
23.56 in numeric(5,3) will save 23.60 (rounded up)  
23.56 in numeric(5,4) will save 23.56

We use PostgreSQL 8.3 and 8.4

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [December 9, 2009, 1:04pm UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/3 "2009-12-09T13:04:50Z")

</div>

do you have a quick example for this?

Can you test also if this is a pure db problem? So can you test on sybase?

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [December 9, 2009, 1:43pm UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/4 "2009-12-09T13:43:44Z")

</div>

Hi,

We cannot test it on other database. But it can be reproduced very easily.  
Here is what you need to do:

- Create database with a table having a column “calculation” of type “numeric(19,2)”
- Create a solution in Servoy 5 linked to this database
- Create a calculation on the table having the name “calculation” (same name as column name to make it stored) and which return a NUMBER
- In this calculation just put the following code “return 345.67”
- In your solution make sure the calculation is run at least once
- Run your solution in Developer
- Check the result stored in the database. Should be 350.00
- Change the database datatype to “numeric(19,5)”
- Restart Developer
- Run again the solution
- Check the result stored in the database. Should be 345.67

Of course you can edit an existing solution to test it as well.  
Also we have checked PostgreSQL doc to make sure we don’t miss-understand the datatype syntax. But it behaves differently than the doc: [http://www.postgresql.org/docs/8.3/inte … IC-DECIMAL](http://www.postgresql.org/docs/8.3/interactive/datatype-numeric.html#DATATYPE-NUMERIC-DECIMAL)

> NUMERIC(precision, scale)  
> The number 23.5141 has a precision of 6 and a scale of 4.

---

<div class="post-metadata">

**Author:** ![jcompagner](https://yyz2.discourse-cdn.com/flex010/user_avatar/forum.servoy.com/jcompagner/32/4889_2.png) [@jcompagner](https://forum.servoy.com/u/jcompagner)\
**Post date:** [December 9, 2009, 2:13pm UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/5 "2009-12-09T14:13:45Z")

</div>

please create a case with this info.

---

<div class="post-metadata">

**Author:** ![Foobrother](https://avatars.discourse-cdn.com/v4/letter/f/50afbb/32.png) [@Foobrother](https://forum.servoy.com/u/Foobrother)\
**Post date:** [December 9, 2009, 2:35pm UTC](https://forum.servoy.com/t/servoy-5-decimal-calculation-issue/11495/6 "2009-12-09T14:35:41Z")

</div>

Case 260294 created
