Decimal to String with Mandatory Length
Tags: code-golfstringfloating-pointconversion
Introduction:
Inspired by this SO question, which asks for the most accurate precision of decimal values using either rounding or scientific notation as string, with at most 15 characters long. This would include the -
, .
, and E
in the output-string.
Challenge:
Inputs:
- A decimal value \$s\$
- An integer output-length \$n\$
Output:
- A string of the most accurate representation of the given decimal value, with a length exactly equal to the output-length \$n\$. NOTE: the linked SO question ask for at most 15 characters long, but this challenge asks for exactly \$n\$ characters long instead.
Challenge rules:
- The output-length input is guaranteed to be \$n\geq7\$
- The decimal input is guaranteed to be valid and non-empty
- You are allowed to take the input-decimal \$s\$ as string
- The input-decimal is guaranteed to only contain the characters
0123456789-.
, and will not start with an .
(but 0.
instead), nor start with unnecessary leading 0
s like 001.23
instead of 1.23
(except for 0.
).
- The output-string is guaranteed to only contain the characters
0123456789-.E
(or e
instead of E
if you choose so). The input-format restrictions mentioned one bullet-point above, do NOT apply for the output-format however!
- If the length of the integer part of a number (including
-
for negative values) is larger than the given output-length: use a scientific notation (with either e
or E
) and rounded precision. I.e. with inputs s = "-987654321987654321.987654321"; n = 15
, the output is supposed to be one of these: ["-987654321.99E9", "-9876543219.9E8", "-98765432199.E7"]
- It is allowed to add leading
0
s to get to length \$n\$ without changing its decimal (base-10) value. I.e. with inputs s = "-123.00"; n = 15
, the output "-00000000000123"
would be valid.
- It is allowed to add trailing
0
s to the decimal parts to get to length \$n\$ without changing its decimal (base-10) value. I.e. with inputs s = "-1.23"; n = 15
, the output "-1.230000000000"
or "-1.2300000000E0"
are both valid. (With inputs s = "-0.123456789123456789"; n = 15
, the only possible output is "-.1234567891235"
.)
- Please specify which rounding type your language is using! (E.g. HALF_UP, HALF_DOWN, HALF_EVEN, BANKERS, etc.) My test cases where this is relevant use HALF_UP.
- If multiple outputs are possible, just output one, multiple, or all of them.
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:
All these test cases will use the output-length as 15
(and rounding mode HALF_UP
):
Input: Possible Outputs:
"987654321987654321.987654321" "987654321.988E9","9876543219.88E8","98765432198.8E7","987654321988.E6"
"-987654321987654321.987654321" "-987654321.99E9","-9876543219.9E8","-98765432199.E7"
"1234567891234567.123456789" "1234567.89123E9","12345678.9123E8","123456789.123E7","1234567891.23E6","12345678912.3E5","123456789123.E4"
"-1234567891234567.123456789" "-1234567.8912E9","-12345678.912E8","-123456789.12E7","-1234567891.2E6","-12345678912.E5"
"0.123456789123456789" ".12345678912346"
"-0.123456789123456789" "-.1234567891235"
"5.5555555555555555555555555" "5.5555555555556"
"-5.5555555555555555555555555" "-5.555555555556"
"123456789123456" "123456789123456"
"-123456789123456" "-123456.78912E9","-1234567.8912E8","-12345678.912E7","-123456789.12E6","-1234567891.2E5","-12345678912.E4"
"123.00" "123.00000000000","0123.0000000000","00123.000000000","000123.00000000","0000123.0000000","00000123.000000","000000123.00000","0000000123.0000","00000000123.000","000000000123.00","0000000000123.0","00000000000123.","000000000000123","123.000000000E0","0123.00000000E0","00123.0000000E0","000123.000000E0","0000123.00000E0","00000123.0000E0","000000123.000E0","0000000123.00E0","00000000123.0E0","000000000123.E0","12.3000000000E1","012.300000000E1","0012.30000000E1","00012.3000000E1","000012.300000E1","0000012.30000E1","00000012.3000E1","000000012.300E1","0000000012.30E1","00000000012.3E1","1.23000000000E2","01.2300000000E2","001.230000000E2","0001.23000000E2","00001.2300000E2","000001.230000E2","0000001.23000E2","00000001.2300E2","000000001.230E2","0000000001.23E2",".123000000000E3","0.12300000000E3","00.1230000000E3","000.123000000E3","0000.12300000E3","00000.1230000E3","000000.123000E3","0000000.12300E3","00000000.1230E3","000000000.123E3","1230.0000000E-1","01230.000000E-1","001230.00000E-1","0001230.0000E-1","00001230.000E-1","000001230.00E-1","0000001230.0E-1","00000001230.E-1","000000001230E-1","12300.000000E-2","012300.00000E-2","0012300.0000E-2","00012300.000E-2","000012300.00E-2","0000012300.0E-2","00000012300.E-2","000000012300E-2","123000.00000E-3","0123000.0000E-3","00123000.000E-3","000123000.00E-3","0000123000.0E-3","00000123000.E-3","000000123000E-3","1230000.0000E-4","01230000.000E-4","001230000.00E-4","0001230000.0E-4","00001230000.E-4","000001230000E-4","12300000.000E-5","123000000.00E-5","1230000000.0E-5","12300000000.E-5","123000000000E-5","123000000.00E-6","1230000000.0E-6","12300000000.E-6","123000000000E-6","1230000000.0E-7","12300000000.E-7","123000000000E-7","12300000000.E-8","123000000000E-8","123000000000E-9"
"-123.00" "-123.0000000000","-0123.000000000","-00123.00000000","-000123.0000000","-0000123.000000","-00000123.00000","-000000123.0000","-0000000123.000","-00000000123.00","-000000000123.0","-0000000000123.","-00000000000123","-123.00000000E0","-0123.0000000E0","-00123.000000E0","-000123.00000E0","-0000123.0000E0","-00000123.000E0","-000000123.00E0","-0000000123.0E0","-00000000123.E0","-000000000123E0","-12.300000000E1","-012.30000000E1","-0012.3000000E1","-00012.300000E1","-000012.30000E1","-0000012.3000E1","-00000012.300E1","-000000012.30E1","-0000000012.3E1","-1.2300000000E2","-01.230000000E2","-001.23000000E2","-0001.2300000E2","-00001.230000E2","-000001.23000E2","-0000001.2300E2","-00000001.230E2","-000000001.23E2","-.12300000000E3","-0.1230000000E3","-00.123000000E3","-000.12300000E3","-0000.1230000E3","-00000.123000E3","-000000.12300E3","-0000000.1230E3","-00000000.123E3","-1230.000000E-1","-01230.00000E-1","-001230.0000E-1","-0001230.000E-1","-00001230.00E-1","-000001230.0E-1","-0000001230.E-1","-00000001230E-1","-12300.00000E-2","-012300.0000E-2","-0012300.000E-2","-00012300.00E-2","-000012300.0E-2","-0000012300.E-2","-00000012300E-2","-123000.0000E-3","-0123000.000E-3","-00123000.00E-3","-000123000.0E-3","-0000123000.E-3","-00000123000E-3","-1230000.000E-4","-01230000.00E-4","-001230000.0E-4","-0001230000.E-4","-00001230000E-4","-12300000.00E-5","-123000000.0E-5","-1230000000.E-5","-12300000000E-5","-123000000.0E-6","-1230000000.E-6","-12300000000E-6","-1230000000.E-7","-12300000000E-7","-12300000000E-8"
TODO: Fix the possible outputs of the other test cases as well:
"0.123" ".12300000000000"
"-0.123" "-.1230000000000"
"1.23" "1.2300000000000"
"-1.23" "-1.230000000000"
"0.000000000000001" "10000000000E-25"
"0" "000000000000000"
All these test cases will use the output-length as 7
(and rounding mode HALF_UP
):
Input: Output:
"987654321987654321.987654321" "9.88E17"
"-987654321987654321.987654321" "-9.9E17"
"1234567891234567.123456789" "1.23E15"
"-1234567891234567.123456789" "-1.2E15"
"0.123456789123456789" ".123457"
"-0.123456789123456789" "-.12346"
"5.5555555555555555555555555" "5.55556"
"-5.5555555555555555555555555" "-5.5556"
"123456789123456" "1.23E14"
"-123456789123456" "-1.2E14"
"123.00" "0000123" or "01.23E2" or "1.230E2"
"-123.00" "-000123" or "-12.3E1"
"0.123" ".123000"
"-0.123" "-.12300"
"1.23" "1.23000"
"-1.23" "-1.2300"
"0.000000000000001" "100E-17" or "1.0E-15"
"0" "0000000",".000000","0.00000","00.0000","000.000","0000.00","00000.0","000000.","0E12345","000E-98"",etc.,etc. :/