Skip to main content
Commonmark migration
Source Link

#05AB1E, 10 bytes

05AB1E, 10 bytes

.ò0¹²g-Ì∍J

is the builtin that already gives the answer for most test cases. 0¹²g-Ì∍J is to correctly append trailing zeroes if necessary.

Try it online.

.ò          # Round the second decimal input to the integer first input amount of decimals
            #  i.e. 10 and 0.1 → 0.1
            #  i.e. 1 and 0.67231 → 0.7
0           # Literal '0'
 ¹          # Get the first integer input
  ²g        # Get the length of the second decimal input
    -       # Subtract them from each other
     Ì      # Increase it by 2 for the "0." part
            #  i.e. 10 and 0.1 → 10-3+2 → 9
            #  i.e. 1 and 0.67231 → 1-7+2 → -8
      ∍     # Extend the "0" to a size of that length
            #  i.e. "0" and 9 → "000000000"
            #  i.e. "0" and -8 → ''
       J    # Join everything together
            #  i.e. 0.1 and "000000000" → 0.1000000000
            #  i.e. 0.7 and '' → 0.7

#05AB1E, 10 bytes

.ò0¹²g-Ì∍J

is the builtin that already gives the answer for most test cases. 0¹²g-Ì∍J is to correctly append trailing zeroes if necessary.

Try it online.

.ò          # Round the second decimal input to the integer first input amount of decimals
            #  i.e. 10 and 0.1 → 0.1
            #  i.e. 1 and 0.67231 → 0.7
0           # Literal '0'
 ¹          # Get the first integer input
  ²g        # Get the length of the second decimal input
    -       # Subtract them from each other
     Ì      # Increase it by 2 for the "0." part
            #  i.e. 10 and 0.1 → 10-3+2 → 9
            #  i.e. 1 and 0.67231 → 1-7+2 → -8
      ∍     # Extend the "0" to a size of that length
            #  i.e. "0" and 9 → "000000000"
            #  i.e. "0" and -8 → ''
       J    # Join everything together
            #  i.e. 0.1 and "000000000" → 0.1000000000
            #  i.e. 0.7 and '' → 0.7

05AB1E, 10 bytes

.ò0¹²g-Ì∍J

is the builtin that already gives the answer for most test cases. 0¹²g-Ì∍J is to correctly append trailing zeroes if necessary.

Try it online.

.ò          # Round the second decimal input to the integer first input amount of decimals
            #  i.e. 10 and 0.1 → 0.1
            #  i.e. 1 and 0.67231 → 0.7
0           # Literal '0'
 ¹          # Get the first integer input
  ²g        # Get the length of the second decimal input
    -       # Subtract them from each other
     Ì      # Increase it by 2 for the "0." part
            #  i.e. 10 and 0.1 → 10-3+2 → 9
            #  i.e. 1 and 0.67231 → 1-7+2 → -8
      ∍     # Extend the "0" to a size of that length
            #  i.e. "0" and 9 → "000000000"
            #  i.e. "0" and -8 → ''
       J    # Join everything together
            #  i.e. 0.1 and "000000000" → 0.1000000000
            #  i.e. 0.7 and '' → 0.7
Source Link
Kevin Cruijssen
  • 136.9k
  • 14
  • 158
  • 399

#05AB1E, 10 bytes

.ò0¹²g-Ì∍J

is the builtin that already gives the answer for most test cases. 0¹²g-Ì∍J is to correctly append trailing zeroes if necessary.

Try it online.

.ò          # Round the second decimal input to the integer first input amount of decimals
            #  i.e. 10 and 0.1 → 0.1
            #  i.e. 1 and 0.67231 → 0.7
0           # Literal '0'
 ¹          # Get the first integer input
  ²g        # Get the length of the second decimal input
    -       # Subtract them from each other
     Ì      # Increase it by 2 for the "0." part
            #  i.e. 10 and 0.1 → 10-3+2 → 9
            #  i.e. 1 and 0.67231 → 1-7+2 → -8
      ∍     # Extend the "0" to a size of that length
            #  i.e. "0" and 9 → "000000000"
            #  i.e. "0" and -8 → ''
       J    # Join everything together
            #  i.e. 0.1 and "000000000" → 0.1000000000
            #  i.e. 0.7 and '' → 0.7