unity3d中Debug.Log函数浮点精度问题

转自:http://blog.sina.com.cn/s/blog_5d76edd80101iwib.html


经常用Debug.Log函数来打印程序中的变量值,发现它显示的小数位数只有一位。
这个实在太低了,像0.0xxxxx这种小数就成了0.0!!!!!!
这误差太大了也!
google了一下,确认是Debug.Log函数的问题,它在把Vector转成字符串的时候截断了。
用下面的方法可以控制精度:

Debug.Log(vec.ToString("f4"))

参考这里:http://answers.unity3d.com/questions/173094/show-vector3-full-float-value.html

When you pass a vector to Debug.Log, I suspect Debug.Log calls the Vector’s ToString()-method for you, in order to make a string value out of the argument that it can write to the console. And the way Vector3.ToString() is implemented simply rounds the values to 1 decimal.

To get around this issue, you can either pass in the vector’s coordinates individually, or you can use the overload of ToString that accepts a format, to determine how many decimal points you want the vector represented in. You can use it like this, for example:

Vector3 point = new Vector3(0.9887f, 1.56789f, 3.09273475f);
Debug.Log(point.ToString("F4"));

Here, the F means you’re talking about Fixed-point, and the 4 means you want 4 decimals. You can pass in any number instead of 4. See http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx for further information on what to pass to ToString(string format).

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值