# How to save calc with two decimals in database

Hello,

I´m having issues with the calculations that sometimes Bubble makes, they are not complicated but sometimes doing “field_1 * field_2” returns too many decimals and as it does not round when saving it, then when exporting the data with 2 decimals we have problems.

For example we do field_1 * field_2 and this returns 2,64684 then when exporting the data we say to export with just two decimals and we´re finding that it does not make the rounding.

Thanks a lot.

What does the “formatting as 1028.58” return?

The formatting as returns two decimals, the problem is that if it´s saved with a lot of decimals then sometimes A + B = C that you can see when saving it returns that C saved is not equal to C rounded.

Maybe you can try to use the :rounded to function?

here’s a documentation reference:

:rounded to
Rounds the first number so that it has X digits after the decimal: 0.234 -> 0.23 if the argument is 2. Note that for 0.5 rounded to a number without decimal will be 1

1 Like

Thanks for that.

I´ve seen it but it does not work for us since we´ve to save always with two decimals.

Bests.

Not sure I understand why it does not work for you.

we´ve to save always with two decimals.

That’s exactly what this function does,no?

Yes but this is a problem: Note that for 0.5 rounded to a number without decimal will be 1

Right, I missed this part.

There’s a hack you can use to manage this, which is not the simplest one (or prettiest one), but does the job.

The sequence would be something as follows:

2. Get the result, and format it as you need (2 signs after a decimal), after what it becomes a text
3. You save that text in the db
4. Next time you pull the value for the calculation, you first get it trough the above mentioned hack (Input converting any data type to any data typ) to convert it back to number and do the calculation, then repeat the steps 2-3.

Let me know if that helps.

Hi there!

Random hack: multiply your values * 100. Then just manipulate your figures accordingly when using them.

Just a quick workaround to get your workflows a flowin’.

Have a lovely day!

Ashley

Seems complicated. I thought it would be an easier way to do this

Thanks a lot guys.

This should work as it is. The note is only for if you set the number of digits to zero, as seen here:

(Edit: example now showing some rounding, doh!)

Some cool workarounds though!

The problem is that it could be zero as well.

Thanks a lot.

For your use case, you want to round to 2 decimals:
0.5 will round to 0.5
1 will round to 1
1.445 will round to 1.45
0 will round to 0

1 Like

Thank you so much @mishav