Revision 793df790
fix #8771 display of average value with minimum amount of significant figures
modules/cdm_dataportal/includes/common.inc  

303  303 
$stat_vals_arr['TypicalUpperBoundary'] = '?'; 
304  304 
} 
305  305  
306 
if($stat_vals_arr['Average'] && $stat_vals_arr['TypicalUpperBoundary'] !== null && $stat_vals_arr['TypicalLowerBoundary'] !== null) { 

307 
// $stat_vals_arr['TypicalUpperBoundary']>_value = 0.123457; 

308 
// $stat_vals_arr['TypicalLowerBoundary']>_value = 0.123456; 

309 
// $stat_vals_arr['Average']>_value = 0.1234565234234; 

310 
statistical_values_adjust_significant_figures($stat_vals_arr['Average'], $stat_vals_arr['TypicalLowerBoundary'], $stat_vals_arr['TypicalUpperBoundary']); 

311 
} 

312  
306  313 
foreach ($stat_vals_arr as $key => $statistical_val) { 
307  314  
308  315 
if ($statistical_val !== NULL) { 
...  ...  
363  370 
} 
364  371  
365  372 
/** 
373 
* Calculates the required precision for the taget value to be significantly different from min and may and rounds it. 

374 
* 

375 
* @param $target 

376 
* The statistical value to be rounded to the least significant precision 

377 
* @param $min 

378 
* The lower bound to calculate the least significant precision 

379 
* @param $max 

380 
* The upper bound to calculate the least significant precision 

381 
* @param int $threshold 

382 
* Per default 1, but can be set to any other value > 0 and < 1 to define 

383 
* another threshold for the transition to the next precision level. 

384 
* E.g. A value of 0.5 will cause values > 50 and <= 500 to be shown with 

385 
* a precision of 2, whereas with a threshold of 1 the values > 10 and <= 100 

386 
* will be shown with a precision of 2 figures 

387 
*/ 

388 
function statistical_values_adjust_significant_figures(&$target, $min, $max, $threshold = 1){ 

389  
390 
$precision = 1; 

391 
if($min>_value !== $max>_value){ 

392 
$precision = floor(log10(abs($max>_value  $min>_value) * (1 / $threshold))); 

393 
// increase precision by one 

394 
$precision += $precision < 0 ?  1 : 1; 

395 
} 

396  
397 
$target>_value = sigFig($target>_value, $precision); 

398 
} 

399  
400 
/** 

401 
* based on an idea taken from https://stackoverflow.com/questions/37618679/formatnumbertonsignificantdigitsinphp#answer48283297 

402 
* 

403 
* @param $value 

404 
* @param $digits 

405 
* 

406 
* @return floatstring 

407 
*/ 

408 
function sigFig($value, $digits, $round_only = true) 

409 
{ 

410 
if ($value == 0) { 

411 
$decimalPlaces = $digits  1; 

412 
} elseif ($value < 0) { 

413 
$decimalPlaces = $digits  floor(log10($value * 1))  1; 

414 
} else { 

415 
$decimalPlaces = $digits  floor(log10($value))  1; 

416 
} 

417  
418 
$answer = ($decimalPlaces > 0) && !$round_only ? 

419 
number_format($value, $decimalPlaces) : round($value, abs($decimalPlaces)); 

420 
return $answer; 

421 
} 

422  
423  
424  
425 
/** 

366  426 
* Used internally in statistical_values() do determine equality of stat_vals_arr values 
367  427 
* 
368  428 
* @param $stat_vals_arr 
Also available in: Unified diff