• 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!

TOPIC: Truncate a number

Truncate a number 3 years 7 months ago #41511

Is there a method in the field attributes to truncate a number to zero decimal places?

I need to truncate, not round.

Thanks in anticipation
The administrator has disabled public write access.

Truncate a number 3 years 7 months ago #41514

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 246
  • Thank you received: 64
If setting the decimal for 'calculations' to zero in the configuration doesn't work you could use the following function
function truncate_float($number, $places) {
        $power = pow(10, $places); 
        if($number > 0){
            return floor($number * $power) / $power; 
        } else {
            return ceil($number * $power) / $power; 
        }
    }

this will truncate numbers to zero decimals when used as below
   // demo
   //in RSForm I would use $pos = $_POST['form']['my-field-name']; to get the field value into a variable, saves a bit of typing
    //I've just added a couple here for this demo
    $pos = 523.4884;
    $neg = -18.88651;
 
//return the truncated numbers to the form field
    $_POST['form']['my-field-name'] = truncate_float($pos, 0);
    $_POST['form']['my-other-field-name'] = truncate_float($neg, 0);
 
//echo statement is not needed unless you want to paste into an online tester to check 
    echo 'Positive number is truncated to ' . $_POST['form']['my-field-name'] . " while negative becomes " .  $_POST['form']['my-other-field-name'];
 
   //result is 523 and -18
This will do the job on form submit but if you want the number truncated on leaving the field you'll need javascript which someone else may be able to help with
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
Last Edit: 3 years 7 months ago by iceferret.
The administrator has disabled public write access.
The following user(s) said Thank You: info48313

Truncate a number 3 years 7 months ago #41517

Thank you.

The problem with setting decimals in calculations is that it rounds rather than truncates

What I really need is for the field to display as truncated (to zero decimal places) on the form itself. It is a calculation field just dividing another input field by 7. So 30/7 = 4.2857142857142864 but I just want it to display in the quantity field as "4".

For the record, this is for a sports club. We have competition prize vouchers that are worth $30. Members can trade their vouchers in for future competition entries (credits), which are $7. They have the option of topping the vouchers up with cash to make their payment a multiple of 7, but can also accept that one $30 voucher = 4 credits.

It's a pity that rounding or truncating is not just a simple form process! :)
Last Edit: 3 years 7 months ago by info48313.
The administrator has disabled public write access.

Truncate a number 3 years 7 months ago #41518

  • iceferret
  • iceferret's Avatar
  • OFFLINE
  • Gold Boarder
  • Posts: 246
  • Thank you received: 64
You can truncate numbers with javascript like this:
<p input type="number" id="demo" </p>
 
<script>
function myFunction() {
  return Math.trunc(13.34569);
}
document.getElementById("demo").innerHTML = myFunction();
</script>

Can't take credit for that , adapted a script I found on't web. How it would need re-writing to get the form field calculated value and return the trunc(), that I couldn't work out, still a javascript novice :(
If you can keep your head when all about you are losing theirs, then you obviously don't understand the situation!
The administrator has disabled public write access.

Truncate a number 3 years 7 months ago #41519

  • qubertman
  • qubertman's Avatar
  • OFFLINE
  • Fresh Boarder
  • Posts: 15
  • Thank you received: 5
See if this will work replacing Number1 with your field name.
({Number1:value} - ({Number1:value} % 7)) / 7
For the calculation display field, make sure it is Number type.
The administrator has disabled public write access.
The following user(s) said Thank You: info48313

Truncate a number 3 years 7 months ago #41520

qubertman wrote:
See if this will work replacing Number1 with your field name.
({Number1:value} - ({Number1:value} % 7)) / 7
For the calculation display field, make sure it is Number type.

Thank you very much. That works brilliantly!
The administrator has disabled public write access.
  • 1

Read this first!

We do not monitor these forums. The forum is provided to exchange information and experience with other users ONLY. Forum responses are not guaranteed.

However, please submit a ticket if you have an active subscription and wish to receive support. Our ticketing system is the only way of getting in touch with RSJoomla! and receiving the official RSJoomla! Customer Support.

For more information, the Support Policy is located here.

Thank you!