ExpressionEngine 7.5 has been Released! Learn More!
I had been using MX Calculate to perform calculations, but it appears the parameters aren’t working with PHP 8. I am trying to multiply a field value by a number and have a result display to 2 decimal points.
{partner_amount} * .97 =
How can I do this? Do I need to use jquery?
Site uses EE 7.4.11.
Include jQuery: Make sure jQuery is included in your project. You can add it in your HTML header if it’s not already included: [removed][removed] HTML and [removed] Add the HTML for your field and the script to perform the calculation: <input type=”text” id=”partner_amount” value=”0”>
[removed] $(document).ready(function(){ function calculate() { let partnerAmount = parseFloat($(‘#partner_amount’).val()); let result = (partnerAmount * 0.97).toFixed(2); $(‘#result’).text(result); }
// Calculate on input change
$('#partner_amount').on('input', calculate);
// Initial calculation
calculate();
It is not required that you use jQuery for this. EE comes with its own set of tools for dealing with formatting and calculations.
{exp:low_variables} var=”partner_amount” value=”{partner_amount}” var=”calculated_amount” value=”{partner_amount * .97 | round:2}” {/exp:low_variables}
The calculated amount is: {calculated_amount}
You don’t necessarily need to use jQuery for this. You can accomplish this directly in PHP using the built-in math functions.
Here’s an example of how you can multiply the {partner_amount} field value by 0.97 and display the result to 2 decimal places:
php
<?php
$partner_amount = 100.00; // Replace with the actual field value
$multiplier = 0.97;
$result = round($partner_amount * $multiplier, 2);
echo $result; // Output: 97.00
?>
Breakdown:
$partner_amount
).$multiplier
).$partner_amount
by the $multiplier
using the *
operator.round()
function to round the result to 2 decimal places.This can be easily integrated into your ExpressionEngine 7.4.11 template, either directly in the template code or in a custom PHP file that you include in your template.
No need for any external libraries or jQuery in this case. The PHP math functions should work just fine to achieve the desired calculation and formatting.
Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.