如何在set sthg random-float 1中设置小数位数?

时间:2019-11-06 21:06:34

标签: netlogo

使用set size random-float 1时如何将小数位数设置为2? 现在我有0.12433242,但我想要0.12(大约)。 我知道有show precision 2,但我不知道如何在该命令中包括它。

谢谢

1 个答案:

答案 0 :(得分:3)

Val,如果在显示命令(如show或print)中使用“ precision”,它将仅影响显示的值,而不会触及实际值。如果在set或let命令中使用“ precision”,它将更改实际的存储值,并将其四舍五入到许多小数位。

您可以尝试以下代码:



to setup

  let x random-float 1
  type "original value has high precision: "  
  print x

  type "shown as low precision but value still high precision: " 
  print precision x 3

  type "confirm we didn't affect the original value: " 
  print x

  ;; actually change the value to 3 decimal places using "precision" in a set command
  set x precision x 3

  ;; confirm that worked
  type "New value actually has fewer decimals: " 
  print x

end