You may remember in first or second grade using expanded form to learn about place value of numbers. It's easier to explain with an example, so consider the number 123
. In expanded form it is represented as 100 + 20 + 3
, which helps a young mind visualize place value. It is reminiscent of how you say it: one hundred (plus) twenty (plus) three.
We can extend this past the units place with decimals: 2.718 => 2 + 0.7 + 0.01 + 0.008
Your challenge is to write a program or function that takes a positive floating point number or zero (assume it is as large or precise as your language can handle; it will not be in scientific notation) or string and prints/returns it in expanded form as explained above.
You need neither spaces between the +
's nor the zero before the decimal point, so the example above could be 2+.7+.01+.008
. Values that would be equal to zero must be omitted (101.01 => 100 + 1 + 0.01
) unless the input is zero (see below).
Values should not have more than one leading zero before the decimal point or any trailing zeroes after it (no-no's: 0060, 0000.2, 30., 30.000, .0400
). The input will conform to this too.
Since first-graders have short attention spans, your code will have to be as short as possible.
Test cases
0 => 0
6 => 6
0.99 => 0.9 + 0.09
24601 => 20000 + 4000 + 600 + 1
6.283 => 6 + 0.2 + 0.08 + 0.003
9000000.0000009 => 9000000 + 0.0000009