Goal
Given an input number, round it off to the nearest number with one significant figure.
Requirements
Input
- A floating point number.
- Assume the input number results in an output within the data type's limits (ie. ignore overflow errors.)
- 0 is an invalid input.
- Numbers that cannot be accurately represented in the floating point data type (eg.
"0.35"
being stored as0.3499999
) do not have to be supported.
Output
- The nearest number that consists of one non-zero digit and any number of zero digits.
- The result must support negative numbers and fractional numbers.
- When the input lies exactly between two possible outputs, round away from zero.
Presentation
The focus is on the calculation rather than the presentation. The output may be a floating point data type. It may be text either in full or in scientific notation. If you find a loophole where presenting a certain way reduces your byte count, kudos to you!
Examples
9
-3000
.2
0.2
-.2
7e12
5e-15
1e0
Test Cases
Input Output
1 1
10 10
17 20
99 100
54321 50000
56789 60000
-123 -100
-789 -800
0.23 0.2
0.25 0.3
-0.25 -0.3
4.56e23 5e23
4.56e-23 5e-23
Scoring
The code with the least byte-count after one week wins.