Ask Your Question

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

asked 2016-02-19 20:24:24 -0500

rxu105 gravatar image

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

Puppet community 4.0

edit retag flag offensive close merge delete

2 Answers

Sort by ยป oldest newest most voted

answered 2016-02-20 19:52:55 -0500

csharpsteen gravatar image

updated 2016-02-20 19:53:24 -0500

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:

edit flag offensive delete link more

answered 2016-02-22 06:45:57 -0500

Henrik Lindberg gravatar image

updated 2016-02-22 06:51:14 -0500

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.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools


Asked: 2016-02-19 20:24:24 -0500

Seen: 39 times

Last updated: Feb 22 '16