Remove rounding and leave natural numbers
-
Hello to all of you!
I am making a time calculator (with hours, minutes and seconds).
When calculating some metrics, the result gives me 0.765 and it rounds it to 1.
I need this type of calculation not to be rounded and to show the natural number, without rounding, so that in the case of decimals such as 0.75, it shows 0 and not a 1.
Thanks a lot ??
-
Hi @chotorren
I hope you are doing good today.
The Timepicker field cannot be used for calculations. Do you use Number fields separately for hours, minutes, and seconds? Could you share your form example so e could take a closer look a this?
Please export your form, upload it to google drive or dropbox, and share a link in your next reply.
Kind Regards,
KrisThanks for your quick response.
I used number fields to calculate separately hours, minutes and seconds.
They’re calculated through an index which I’ve named “Cálculo MARCA”, in which the hours field (HORAS MARCA) has the same value of the index.Here you have an example image of the calculation with the code of the form in which the real number of the index should be 0, if I configure it without decimals. As you can see in the field below, the value that it shows is 1.
I hope it helps you to understand my issue. Thanks a lot!
Hi @chotorren
Thank you for response and sharing the form!
I checked it but I’d appreciate a bit more help from you. Form is quite complex and has multiple calculations so which specific calculation(s) this is about?
Could you share some example of data to fill-in the form with to replicate this issue the way you are experiencing it?
It would help a lot!
Best regards
AdamThe calculations I was referring to, despite the form having many fields, were the same as those shown in the image I attached in the link.
Anyways, I have duplicated the form, with only the fields I am referring to, with the fields in English as well, to make it easier for you. There you are the updated data!
https://justpaste.it/forminator-chotorren
As you can see in the image, we are trying to calculate the hours, minutes and seconds based on a given distance and pace.
There is an “INDEX” field which is the index with which we subsequently calculate the hours, minutes and seconds.
The result we should get in the end, with the values we have put in the image, is 0h, 16min, 10sec.
The value we have put in the calculation of “Hours” is the same as the index, in which we simply remove the decimals and a 0 should appear. But instead a 1 appears, making a domino effect that also influences the subsequent calculations of “minutes” and “seconds”, even giving negative results that do not make sense.
That is why we need to know if there is a way of not rounding the calculations and displaying only the natural number. In this case, a 0.
I hope this explanation is clearer now. Thank you a lot!!!
Hi @chotorren
I am afraid we are getting:
Oops!
An unexpected error occurred
Administrator has been informed about the error. You can also:From https://justpaste.it/forminator-chotorren can you please use https://pastebin.com/ instead?
Best Regards
Patrick FreitasHi @chotorren,
Thanks for sharing the form export; I’m escalating this further to our developer’s attention to see if there is any workaround that could be shared.
Will keep you updated once we get further feedback.
Kind Regards,
NithinHi @chotorren,
Could you please try the following snippet and see whether it helps:
<?php /** * Plugin Name: [Forminator Pro] - Round calc value. * Description: [Forminator Pro] - Round calc value, e.g 22.45 => 22.50. * Author: Thobk & Prashant @ WPMUDEV * Author URI: https://premium.wpmudev.org * License: GPLv2 or later */ if ( ! defined( 'ABSPATH' ) ) { exit; } elseif ( defined( 'WP_CLI' ) && WP_CLI ) { return; } add_action( 'after_setup_theme', 'wpmudev_fm_round_calc_value', 100 ); function wpmudev_fm_round_calc_value() { if ( defined('FORMINATOR_PRO') && class_exists( 'Forminator' ) ) { class WPMUDEV_Round_Calc_Value{ public $additional_css_class = 'wpmu-dec-change';//Enter class name and set it in the settings field to apply only for specific field. private $field; public function __construct(){ add_filter( 'forminator_field_calculation_calculated_value', array( $this, 'round_calc_value' ), 10, 4 ); add_action( 'wp_footer', array( $this, 'custom_script' ), 9999 ); } public function custom_script(){ ?> <script> (function($){ $(function(){ function roundCalcValue( _form ){ if( _form.length ){ _form.find('.wpmu-dec-change input').on('change', function(){ let _this = $(this), _val = _this.val(); if( ! _this.data('fm-changing') && ( _val % 1 ) !== 0 && _val.indexOf('.') > -1 ){ _this.data('fm-changing', 1); setTimeout(function(){ _this.val( Math.trunc(_val) ).trigger('change'); }, 50); } setTimeout(function(){ _this.removeData('fm-changing'); }, 200); }); } } roundCalcValue( $('.forminator-custom-form') ); // ajax. $(document).on('after.load.forminator', function( e, form_id ){ roundCalcValue( $('.forminator-custom-form-'+ form_id ) ); }); }); })(window.jQuery) </script> <?php } public function round_calc_value( $result, $converted_formula, $submitted_data, $field_settings ){ if( ! empty( $this->additional_css_class ) ){ if( ! empty( $field_settings['custom-class'] ) && strpos( $field_settings['custom-class'], $this->additional_css_class ) !== false ){ $result = intval($result); } } return $result; } } $run = new WPMUDEV_Round_Calc_Value(); } }
You can add the above snippet as a mu-plugins. Please check this link on how to implement the above code as a mu-plugins:
https://wpmudev.com/docs/using-wordpress/installing-wordpress-plugins/#installing-mu-pluginsOnce the snippet is added, you’ll need to add the following class name “wpmu-dec-change” to all the calculation fields.
Screenshot:
And also, make sure the “Separators” and the “Round to” have the following format for all the calculation fields:
Please do let us know how that goes.
Kind Regards,
NithinThank you very much for your time.
Unfortunaltely the calculation fields seem to be the same as before and still show that “1” (rounded up) if I set that field without decimals.
All the calculations work fine if the fields are set with 2 decimals. But what I need is to show only the integer part of the number (in this case a “0”).
Actually, I solved this isse with another plugin that works perfectly.
But I still want to know if this is possible to do with Forminator, for I am crazy in love with Forminator ??
Sure, that hundreds of Forminator users will have an issue like that and appreciate a solution to this thread.
Once more, thanks a lot for your time and dedication.
Roberto
Hi @chotorren
I hope you are doing well
“Actually, I solved this isse with another plugin that works perfectly.”
Could you let us know the plugin so we can take a closer look?
Best Regards
Patrick FreitasHello @chotorren ,
We haven’t heard from you for some time now, so it looks like you don’t have any more questions for us.
Feel free to re-open this ticket if needed.
Kind regards
Kasia
- The topic ‘Remove rounding and leave natural numbers’ is closed to new replies.