| 1 |
<?php |
| 2 |
defined( 'ABSPATH' ) or die( 'Cheatin\' uh?' ); |
| 3 |
|
| 4 |
/** |
| 5 |
* Round UP to nearest half integer |
| 6 |
* |
| 7 |
* @since 1.0 |
| 8 |
* @source http://stackoverflow.com/a/13526408 |
| 9 |
* |
| 10 |
* @param int $number The number to round up |
| 11 |
* @return int $number The formatted number |
| 12 |
*/ |
| 13 |
function imagify_round_half_five( $number ) { |
| 14 |
$number = strval( $number ); |
| 15 |
$number = explode( '.', $number ); |
| 16 |
|
| 17 |
if ( ! isset( $number[1] ) ) { |
| 18 |
return $number[0]; |
| 19 |
} |
| 20 |
|
| 21 |
$decimal = floatval( '0.' . substr( $number[1], 0 , 2 ) ); // cut only 2 number |
| 22 |
if ( $decimal > 0 ) { |
| 23 |
if( $decimal <= 0.5 ) { |
| 24 |
return floatval( $number[0] ) + 0.5; |
| 25 |
} elseif ( $decimal > 0.5 && $decimal <= 0.99 ) { |
| 26 |
return floatval( $number[0]) + 1; |
| 27 |
} |
| 28 |
} else { |
| 29 |
return floatval( $number ); |
| 30 |
} |
| 31 |
} |