Home > OS >  Is there a PHP function similar to (int) that will allow me to call rational numbers?
Is there a PHP function similar to (int) that will allow me to call rational numbers?

Time:03-26

I have been doing some reading and, being new to PHP, cannot seem to understand how to correctly use rational numbers within PHP (specifically wordpress functions.php file).

On a site I have created, users enter a test score $straight_acceleration - which typically consists of rational numbers, and not integers (eg. 1.27 seconds) - and I capture it as an ACF value in the database.

I then run a calculation against this test score in the funtions.php file of my child theme to give them a rating out of 100.

In the below code I have used the (int) function to call the value, but this obviously doesn't work as it converts the value to an integer, outputting the incorrect rating. If I remove the (int) function, it throws a fatal error.

Is there a function that i need to use (similar to the (int) function) that will allow me to call rational numbers? Or is there something else wrong with my code that is not allowing me to remove the (int) function?

Any help would be appreciated.

 $current_user = wp_get_current_user();
    
    $straight_acceleration = get_user_meta($current_user->ID, 'straight_acceleration', true);
    
        if ((int)$straight_acceleration >= 1.14) {
    
        $straight_acceleration_percent_value = (100*((0.2)   ((((int)$straight_acceleration - 1.27)*(0.6-0.2))/(1.14 - 1.27))));
    
        $min = 0;
        $max = 100;
        $current = $straight_acceleration_percent_value;
        $clamped_straight_acceleration_percent_value = max($min, min($max, $current));
        
        $clamped_straight_acceleration_percent_value = round($clamped_straight_acceleration_percent_value, 2);
            
        update_user_meta($current_user->ID, 'straight_acceleration_percent', $clamped_straight_acceleration_percent_value);

    }

CodePudding user response:

Maybe you are looking for floatval? It should be doing what you need, it converts a string like "10.2" to a pure float number like 10.2

The only thing you should aware of is that float numbers are locale-dependant. That means that if your locale is US then you have point as decimal separator, if you have something like IT then you may have comma as decimal separator

If you get a fatal out of your calculations it maybe because you are trying to do math operations between strings and pure numbers (which can be float or as you call them rational)

CodePudding user response:

In php u can also compare string number directely (int , float , double )

$string_val = "1.156";
$float_val = 1.156;

echo '<br>';
echo ($string_val > 1)?"bigger":"smaller"; // bigger 
echo '<br>';
echo ($string_val > 1.5)?"bigger":"smaller"; // smaller
echo '<br>';
echo ($string_val > "1")?"bigger":"smaller";// bigger 
echo '<br>';
echo ($string_val > '1.5')?"bigger":"smaller";// smaller
echo '<br>';
echo ($float_val > 1)?"bigger":"smaller";// bigger 
echo '<br>';
echo ($float_val > 1.5)?"bigger":"smaller";// smaller
echo '<br>';
echo ($float_val > "1")?"bigger":"smaller";// bigger 
echo '<br>';
echo ($float_val > '1.5')?"bigger":"smaller";// smaller
echo '<br>';
  • Related