How to find length of digits in a number in Netlogo?
Solution 1:
You can use word
to get from whatever input to a string:
to-report digits-count [number]
if (not is-number? number) [
error "The input passed to 'digits-count' was not a number!"
]
report length (word number)
end
; Then, in the Command Center:
observer> show digits-count 123438
observer: 6
Note that, if it is possible to have numbers with characters that are not digits (e.g. a decimal separator, a minus sign etc) you need to also remove those characters using remove
:
to-report digits-count [number]
if (not is-number? number) [
error "The input passed to 'digits-count' was not a number!"
]
let string (word number)
set string (remove "." string)
report length string
end
; Then, in the Command Center:
observer> show digits-count 234.45
observer: 5