Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert string to float?

Tags:

java

Just as title says. I don't believe it is possible to do this but if it is let me know.

This is needed for a bukkit (minecraft server) plugin I'm writing. I want to take a command: tnt [power]. Where power is the string returned that I want to convert to float.

Thanks

like image 325
HcgRandon Avatar asked Jan 02 '12 20:01

HcgRandon


People also ask

Can you turn a string into a float?

We can convert a string to float in Python using the float() function. This is a built-in function used to convert an object to a floating point number. Internally, the float() function calls specified object __float__() function.

What converts a string to floating-point number?

The atof() function converts a character string to a double-precision floating-point value. The input string is a sequence of characters that can be interpreted as a numeric value of the specified return type.

Why can I not convert string to float?

The Python "ValueError: could not convert string to float" occurs when we pass a string that cannot be converted to a float (e.g. an empty string or one containing characters) to the float() class. To solve the error, remove all unnecessary characters from the string.

How do I convert a float to a string in Python?

In Python, we can use str() to convert float to String.


1 Answers

Use Float.valueOf(String) to do the conversion.

The difference between valueOf() and parseFloat() is only the return. Use the former if you want a Float (object) and the latter if you want the float number.

like image 159
Francis Upton IV Avatar answered Sep 23 '22 18:09

Francis Upton IV