Asked  7 Months ago    Answers:  5   Viewed   43 times

I found this code on php.net which works for me for the round_up part..

function round_up($value, $precision = 0) { 
 if (!empty($value)) {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (+1 == $sign) { 
             $amt[1] = $amt[1] + (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';} 
 }

Added a couple minus's.. to get this one...

function round_down($value, $precision = 0) { 
 if (!empty($value))  {
 $sign = (0 <= $value) ? +1 : -1; 
 $amt = explode('.', $value); 
 $precision = (int) $precision; 
 if (strlen($amt[1]) > $precision) { 
     $next = (int) substr($amt[1], $precision); 
     $amt[1] = (float) (('.'.substr($amt[1], 0, $precision)) * $sign); 
     if (0 != $next) { 
         if (-1 == $sign) { 
             $amt[1] = $amt[1] - (float) (('.'.str_repeat('0', $precision - 1).'1') * $sign); 
         } 
     } 
 } 
 else { 
     $amt[1] = (float) (('.'.$amt[1]) * $sign); 
 } 
 return $amt[0] + $amt[1]; 
 }
 else {echo 'error';}
 } 

Is there any better way to do it? (I only require it for positive decimals) Currently for the most part it works without much glitches..

$azd = 0.0130; 

$azd1 = number_format(round_up($azd,2),4);
$azd2 = number_format(round_down($azd,2),4);
$azd3 = number_format(round_up($azd,1),4);
$azd4 = number_format(round_down($azd,1),4);

echo 'Round_up = '.$azd1.'<br>'; // 0.0200
echo 'Round_down = '.$azd2.'<br>'; // 0.0100
echo 'Round_up = '.$azd3.'<br>';    // 0.1000
echo 'Round_down = '.$azd4.'<br>';  // 0.0000

 Answers

15
function round_up($value, $precision=0) {
    $power = pow(10,$precision);
    return ceil($value*$power)/$power;
}
function round_down($value, $precision=0) {
    $power = pow(10,$precision);
    return floor($value*$power)/$power;
}
Saturday, May 29, 2021
 
RenegadeAndy
answered 7 Months ago
57

So you're saying you have

id data1 data2 data3
1  2.5   2.6   2.7
2  2.6   7.0   8.2
3  3.0   1.8   6.0

and you want to compute the product of the main-diagonal entries (disregarding the id column)?

It's not clear from your question whether this is what you want to do, or whether you just want to compute data1 * data2 * data3 for each row.

If it's the latter: You should do this in MySQL. Add an extra column to the column list in your select statement:

SELECT
    id,
    ...,
    data1 * data2 * data3 AS product
FROM
    ...

If it's the former: You should do this in PHP rather than MySQL. You can do this by setting up a multidimensional array.

You probably have code that looks something like this:

while ( $row = mysqli_fetch_assoc($query_result) ) {
    echo $row['id'].' '.$row['data1'].' '.$row['data2'].' '.$row['data3'];
}

We will change it to the following:

$myarray = array();
while ( $row = mysqli_fetch_assoc($query_result) ) {
    $myarray[] = array($row['data1'], $row['data2'], ['data3']);
}

Now if you add the following:

echo '<pre>';
var_dump($myarray);
echo '</pre>';

you will see we have a two-dimensional array.

Now if we want to find the product of the diagonal entries in that array we can use this:

$product = $myarray[0][0] * $myarray[1][1] * $myarray[2][2];
Saturday, May 29, 2021
 
Xavio
answered 7 Months ago
62

Seems like you need the floor:

import math
math.floor(a * 100)/100.0

# 28.26
Friday, June 18, 2021
 
anjan
answered 6 Months ago
82

I think the Java language's assert keyword is likely what you want. Under the covers, the assert keyword essentially compiles into Dalvik byte code that does two things:

  1. Checks whether the static variable assertionsDisabled (set in the class' static constructor via a call to java.lang.Class.desiredAssertionStatus()) is != 0 and if so, does nothing
  2. If it is 0, then it checks the assertion expression and throws a java.lang.AssertionError if the expression resolves to false, effectively terminating your application.

The Dalvik runtime by default has assertions turned off, and therefore desiredAssertionStatus always returns 1 (or more precisely, some non-zero value). This is akin to running in "retail" mode. In order to turn on "debug" mode, you can run the following command against the emulator or the device:

adb shell setprop debug.assert 1

and this should do the trick (should work on the emulator or any rooted debugging-ready device).

Note however that the aforementioned Dalvik code that checks the value of assertionsDisabled and throws an AssertionError if the expression is false is always included in your byte code and liberal sprinkling of asserts in your code may lead to byte code bloat.

Please see this for a bit more detail: Can I use assert on Android devices?

Thursday, July 29, 2021
 
laurent
answered 5 Months ago
22

add these code into the top of your script

@set_magic_quotes_runtime(false);
ini_set('magic_quotes_runtime', 0);
Wednesday, August 18, 2021
 
Warrior
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :
 
Share