Input:
A decimal
Output:
The input truncated
How to truncate them?
The integer part of the input will remain the same. The decimal part of the input will be truncated based on the lowest digit in consecutive order. The two step-by-step examples below should clarify what I mean by that:
Example 1: Input: 17.46940822
Step 1: Leave the integer part unchanged: 17
Step 2: Look at the first decimal digit, and truncate it to that many decimal digits: 4
→ .4694
Step 3: Look at the next decimal digit. If it's lower, adjust the truncation accordingly. If not, continue to the next decimal digit: 6
→ remains .4694
Step 3 again: 9
→ remains .4694
Step 3 again: 4
→ remains .4694
So the output will be: 17.4694
.
Example 2: Input: 10.4521084
Step 1: Leave the integer part unchanged: 10
Step 2: Look at the first decimal digit, and truncate it to that many decimal digits: 4
→ .4521
Step 3: Look at the next decimal digit. If it's lower, adjust the truncation accordingly. If not, continue to the next decimal digit: 5
→ remains .4521
Step 3 again: 2
→ truncation adjusted to: .45
So the output will be: 10.45
Challenge rules:
- You are allowed to take the input as decimal, string, list/array/stream of characters, etc. (Same applies to the output, although I doubt there are a lot of languages that have the proper truncation while remain working with decimal values.)
You are also allowed to use a comma,
instead of a dot.
(perhaps useful for regex-based solution/languages). - If the decimal part is not long enough for the truncation, you are optionally allowed to add trailing zeros. I.e.
123.4
can be both123.4
or123.4000
- If you have to truncate to
0
decimal digits we omit the decimal digits. The dot/comma may remain optionally if you use strings. I.e.847.3072
→847
(or847.
) - You can assume the input will always contain at least one decimal digit, so
123
won't be an input, but123.0
can be an input.
General rules:
- This is code-golf, so shortest answer in bytes wins.
Don't let code-golf languages discourage you from posting answers with non-codegolfing languages. Try to come up with an as short as possible answer for 'any' programming language. - Standard rules apply for your answer with default I/O rules, so you are allowed to use STDIN/STDOUT, functions/method with the proper parameters and return-type, full programs. Your call.
- Default Loopholes are forbidden.
- If possible, please add a link with a test for your code (i.e. TIO).
- Also, adding an explanation for your answer is highly recommended.
Test cases:
Input: Output:
17.46940822 17.4694
10.4521084 10.45
123.4 123.4000 (or 123.4)
-847.3072 -847 (or -847.)
123.0 123 (or 123.)
98.76543210 98.7654
0.3165 0.3
-0.1365 -0.1
82937.82937 82937.82
-0.1010101 -0.1
-1.0101010 -1 (or -1.)
28.684297 28.68
13.35 13.350 (or 13.35)
0.99999999999 0.999999999
28.321 28.32
0.56462 0.5646
0.56462
(This Jelly code passes all other given test cases :() \$\endgroup\$