# Why does the statement "008" + 0 fail saying it can't convert to numeric?

"001" + 0 to "007" + 0 does work but "008" + 0 "009" + 0 does not

Puppet community 4.0

edit retag close merge delete

Sort by ยป oldest newest most voted

In Puppet 4, a number with a leading 0 is interpreted as octal digits which have valid values of 0 through 7. Therefore, digits above 7 cause conversion errors when strings with a leading 0 are coerced to numbers.

Complete details about Octal and Hexadecimal representations in Puppet can be found in the Language section of the documentation:

more

As noted this is because the automatic conversion takes a leading '0' to mean that the value is using base 8 (octal) instead of 10, and an octal digit can not be greater than 7. The issue can be solved by using the scanf function to convert the value with a specified radix. e.g.

$str = "008"$val = \$str.scanf("%d")[0]


And this will return an Integer without the need of adding a 0 to force coercion to integer. The scanf function can convert several values at once and it always returns an array; hence the need for the '[0]' at the end.

more