Netlogo - add one more digit to the string automatically

别说谁变了你拦得住时间么 提交于 2019-12-11 17:19:44

问题


I am modelling a warehouse. each cell has been assigned to a id in a format like below.

The standard format of the external data is four digits. However, there are some ids which are only 3 like "A05". Is there a easy way to automatically add one more "0' to make it like "A005"?


回答1:


Maybe this reporter will work for you- it should just add a zero in the second position until the string length is 4 or greater.

to setup
  ca
  print map add-zero [ "A" "A1" "A01" "A001" "A123" ] 
  reset-ticks
end

to-report add-zero [ string ]
  if length string >= 4 [
    report string
  ]
  report add-zero insert-item 1 string "0"  
end


来源:https://stackoverflow.com/questions/55205963/netlogo-add-one-more-digit-to-the-string-automatically

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!