RosettaCodeData/Task/Middle-three-digits/Pluto/middle-three-digits.pluto
2026-04-30 12:34:36 -04:00

19 lines
548 B
Text

local fmt = require "fmt"
local function middle3(n)
if n < 0 then n = -n end
local s = tostring(n)
local c = #s
if c < 3 then return $"Minimum is 3 digits, only has {c}." end
if c % 2 == 0 then return $"Number of digits must be odd, {c} is even." end
if c == 3 then return s end
local d = (c - 1) / 2
return s:sub(d, d + 2)
end
local a = {123, 12345, 1234567, 987654321, 10001, -10001, -123, -100, 100, -12345,
1, 2, -1, -10, 2002, -2002, 0}
for a as e do
fmt.print("%9d -> %s", e, middle3(e))
end