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

How to set the number of decimals in set sthg random-float 1?

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

谢谢

Val,如果在显示命令中使用"precision",如show 或print,它只影响显示值而不影响实际值。如果您在 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