处理 float() 函数 [英] Processing float() function

查看:34
本文介绍了处理 float() 函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,这个函数是专门用于处理的还是java中存在的默认?

First of all, is this function special to processing or does it exist in java by default?

当我编码处理下面的行时,

When I code in processing the line below,

println(float("88") + "\t" + float("88\n") + "\t" + float("88p") ); // p is just an arbitrary character other than '\n'

输出

88.0 88.0 NaN

那么,为什么 float() 函数适用于 '\n' 字符但不适用于 'p' ?他们不是两个角色吗?我知道 '\n' 很特别,但在这种情况下它有什么不同吗?

So, why float() function works fine with '\n' character but does not work with 'p' ? Aren't they both characters? I know '\n' is something special but in this case does it make a difference?

由于答案中出现了一些警告,我已将K"替换为p".

edit : I have replaced 'K' with 'p' because of some warnings came from the answers.

推荐答案

它只存在于处理中.如果您想在 Java 中使用类似的功能,可以使用 Float#parseFloat(String)

It exists only in processing. If you wanted a similar function in Java, there's Float#parseFloat(String)

我认为它适用于 \n,因为这是一个换行符,它是空格.float 可能会在解析值之前修剪空格.K 不是空白字符也不是数字,因此无法解析为浮点数.

I assume it works with \n, because that's a newline character which is whitespace. float likely trims whitespace before parsing the value. K is not a whitespace character and not a digit, so it cannot be parsed to a float.

这篇关于处理 float() 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆