CalcBuilder Forum

Working with decimals

User anonymous 2011-10-21 14:30:46

Hello,

If purchased the Calc builder.
It is a very nice program exactly what i was looking for.

I got one question.
When i want to work with numbers with decimals (for example 1.5) it works fine.
however it pretty common that people uses 1,5.

Then the program can't calculate it.

Is there a work around for this?
Maybe something in PHP were i can convert it to 1.5 so the program can calculate with the number?

thank you in advance.

Kind regards,

Moonsoft support 2011-10-21 14:31:41

Hello,

In PHP you can change 1,5 or 1.254,5 in several ways. One easy way is this:

Let´s suppose we want to get a number with this formats : 1.254 or 1.254,10 or 25141,41
We can declare user field in backend as number or text. In this case the "variable" of the field we call as "money".
What we need to do in code?
- Delete all '.'
// with str_replace we replace all . by nothing.
// You can see doc in http://www.php.net/manual/en/function.str-replace.php
// First param is search, second is replaced and third is subject
$first_step=str_replace( '.' , '' , $money);
- Now we can change ',' by '.'
// We can use the same function
// In this second step, our subject is first_step.
$second_step=str_replace( ',' , '.' , $first_step);
- We just finished. In $second_step we have a number in format 0000.00

PHP detects, when you use it in operations, if variables are numbers or strings. Nevertheless you can use "floatval" funtion to ensure you are using a number (in this case float).

// Function docs in http://www.php.net/manual/en/function.floatval.php
$iam_number=floatval($second_step);


Hope this helps,

Moonsoft support 2016-05-30 14:27:08

Hello,

I have tried the abovementioned method but it obviously doesn't do what I would like:

I have a field where will enter a number in a EU form (using commas for decimals).

If a number is entered in my form in a "xxx.xx" format, the result is correct.
If a number is entered in a "xxx,xx format", the calculator calculates a xx,xxx.xx number instead, thus rendering a false result.

How could I set the parameters of the calculator so that a number in a "xxx,xx" form would be properly used (using a comma and not a dot for decimals).

Thanks for your help!

Moonsoft support 2016-05-30 17:15:59

Hello,
well, the above solution was based on code because it's from many years ago and its a code-base solution. With the new versions of Calcbuilder there is an easy way to configure the decimal separator you will allow to use for the front-end. Go to your calculator->preferences tab and scroll down to the section 'thousand and decimal separators', change the decimal separator to comma, and the thousand separator to dot in order to allow the numbers as you need for the calculation.

Hope this helps, regards

Moonsoft support 2016-06-02 08:24:15

Thanks.

I overlooked that setting, obviously :)

Edited by kreatik - 02.06.2016 08:24

...
List Manager

Build different lists for your site

Buy now!
...
Support/development 40 hours

With the peace of mind of having a professional team at your service (20% discount)

Buy now!