decimal - PHP Scientific Notation Shortening -
i'm looking elegant solution here (if 1 exists).
i've got bunch of numbers, arbitrary amount of decimal places. want force number use 8 decimal places if it's got more 8 trailing zeroes i.e.
0.000000004321
that converted to:
0.00000001
but don't want use number format because if force 8 decimals number format numbers without 8 decimal places like:
1.00000000
i'd rather these (for amounts >= 1):
1.00700000 -> 1.01
and amounts < 1:
0.00300000 -> 0.003
if number less 1 i.e. 0.xx want have more precision (up 8, chopping off trailing 0's).
so, again, here's few examples make clear:
1.00300000 -> 1.01 (maintain , round 2p amounts >= 1) 0.00300000 -> 0.003 (maintaining precision, removing trailing 0's amounts < 1) 1.00300001 -> 0.01 (maintain , round 2dp amounts >= 1) 0.00300001 -> 0.00300001 (no change needed on this) 0.003000017 -> 0.00300002 (rounds upward 8dp because it's < 1) 1.000000002 -> 1.00 (maintain , round 2dp amounts >= 1) 0.000000002 -> 0.00000001 (rounds upward 8dp because it's < 1) at moment small numbers showing in scientific notation because of number of decimals. that's thing need worry (i.e. converting out of scientific notation calculations).
i know can bunch of generic logic, seems hacky way of doing things, surely there's nice solution this.
thank you.
to round upto 2nd place: ceil()
to remove ending zeros: rtrim()
if($number >= 1) { ceil 2nd decimal place } else //when number less 1 { ceil 8th place try rtrim '0' remove ending zeros if there }
Comments
Post a Comment