We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

How to multiply field value by number

How Do I?

Susan Wohlgemuth's avatar
Susan Wohlgemuth
5 posts
about 11 months ago
Susan Wohlgemuth's avatar Susan Wohlgemuth

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.

       
Micah Kohne's avatar
Micah Kohne
4 posts
10 months ago
Micah Kohne's avatar Micah Kohne

{exp:mx_calc expression="{partner_amount}*.97 "} should work.

Testing locally using PHP 8.2.0

       
obscenedisgusted's avatar
obscenedisgusted
2 posts
10 months ago
obscenedisgusted's avatar obscenedisgusted

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();

snow rider

       
hoopigoldberg's avatar
hoopigoldberg
2 posts
9 months ago
hoopigoldberg's avatar hoopigoldberg

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} zc

The calculated amount is: {calculated_amount}

       
mucketymuck's avatar
mucketymuck
1 posts
9 months ago
mucketymuck's avatar mucketymuck

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:

  1. Assign the {partner_amount} field value to a PHP variable (e.g., $partner_amount).
  2. Define the multiplier value (in this case, 0.97) as a separate variable (e.g., $multiplier).
  3. Multiply the $partner_amount by the $multiplier using the * operator.
  4. Use the round() function to round the result to 2 decimal places.
  5. Output the final result.

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.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.