For converting a string to float you can use float(). Eg.float('1.2123') You can not convert '*' into float because its an operator that's why error is coming.
#observe the code
volume = float(input("Enter volume of sphere in mm^3: "))
input() = Takes input from user and stores it in string
float() = It is used to convert strings to float
Your code is volume = float(input("Enter volume of sphere in mm^3: "))
1st input() takes input from user in string then float() converts it into float.
But a = 1.4*1.0e6 there is no input() function involved thats why it's a float.
You can simply achieve your work by
volume = eval(input("Enter volume of sphere in mm^3: "))
OR
volume = input("Enter volume of sphere in mm^3: ").split("*")
volume = float(volume[0])*float(volume[1])
split(separator)- It splits a string into an array of sub-strings, by a separator string provided by user
You can read about eval from here:- Read abut eval() from here
Answer from Abhay Kumar on Stack OverflowHow to convert string to float in Python? Why is it a float when directly assigned but a string when a user inputs it? - Stack Overflow
How Do I Convert String To Float?
Convert String to Float - Questions & Answers - Unity Discussions
python - How do I parse a string to a float or int? - Stack Overflow
Videos
For converting a string to float you can use float(). Eg.float('1.2123') You can not convert '*' into float because its an operator that's why error is coming.
#observe the code
volume = float(input("Enter volume of sphere in mm^3: "))
input() = Takes input from user and stores it in string
float() = It is used to convert strings to float
Your code is volume = float(input("Enter volume of sphere in mm^3: "))
1st input() takes input from user in string then float() converts it into float.
But a = 1.4*1.0e6 there is no input() function involved thats why it's a float.
You can simply achieve your work by
volume = eval(input("Enter volume of sphere in mm^3: "))
OR
volume = input("Enter volume of sphere in mm^3: ").split("*")
volume = float(volume[0])*float(volume[1])
split(separator)- It splits a string into an array of sub-strings, by a separator string provided by user
You can read about eval from here:- Read abut eval() from here
ValueError: could not convert string to float: '1.4*1.0e6'
you try to convert formula to float. the most easy way is convert string to formula using eval
volume = eval(input("Enter volume of sphere in mm^3: "))
>>> a = "545.2222"
>>> float(a)
545.22220000000004
>>> int(float(a))
545
Python2 method to check if a string is a float:
def is_float(value):
if value is None:
return False
try:
float(value)
return True
except:
return False
For the Python3 version of is_float see: Checking if a string can be converted to float in Python
A longer and more accurate name for this function could be: is_convertible_to_float(value)
What is, and is not a float in Python may surprise you:
The below unit tests were done using python2. Check it that Python3 has different behavior for what strings are convertable to float. One confounding difference is that any number of interior underscores are now allowed: (float("1_3.4") == float(13.4)) is True
val is_float(val) Note
-------------------- ---------- --------------------------------
"" False Blank string
"127" True Passed string
True True Pure sweet Truth
"True" False Vile contemptible lie
False True So false it becomes true
"123.456" True Decimal
" -127 " True Spaces trimmed
"\t\n12\r\n" True whitespace ignored
"NaN" True Not a number
"NaNanananaBATMAN" False I am Batman
"-iNF" True Negative infinity
"123.E4" True Exponential notation
".1" True mantissa only
"1_2_3.4" False Underscores not allowed
"12 34" False Spaces not allowed on interior
"1,234" False Commas gtfo
u'\x30' True Unicode is fine.
"NULL" False Null is not special
0x3fade True Hexadecimal
"6e7777777777777" True Shrunk to infinity
"1.797693e+308" True This is max value
"infinity" True Same as inf
"infinityandBEYOND" False Extra characters wreck it
"12.34.56" False Only one dot allowed
u'ๅ' False Japanese '4' is not a float.
"#56" False Pound sign
"56%" False Percent of what?
"0E0" True Exponential, move dot 0 places
0**0 True 0___0 Exponentiation
"-5e-5" True Raise to a negative number
"+1e1" True Plus is OK with exponent
"+1e1^5" False Fancy exponent not interpreted
"+1e1.3" False No decimals in exponent
"-+1" False Make up your mind
"(1)" False Parenthesis is bad
You think you know what numbers are? You are not so good as you think! Not big surprise.
Don't use this code on life-critical software!
Catching broad exceptions this way, killing canaries and gobbling the exception creates a tiny chance that a valid float as string will return false. The float(...) line of code can failed for any of a thousand reasons that have nothing to do with the contents of the string. But if you're writing life-critical software in a duck-typing prototype language like Python, then you've got much larger problems.
Hi all, I have a string a = '1721244344.700249000', I want to convert it to a floating value.
Float() is returning only 2 places after decimal point. Like 1721244344.7
Is there a way I can convert the entire string to a floating point value and get all the decimal (upto 9 places after decimal point)?
I have to use python v2.7 for this.
Edit: I do not have problem in printing the all 9 decimal places but I need the floating value so that I can subtract another value so that I get the difference with accuracy upto 9 th decimal point.
I need to put a String in sePrice(float price) setter, but then I get this error: Exception in thread "JavaFX Application Thread" java.lang.ClassCastException: class java.lang.Float cannot be cast to class java.lang.String (java.lang.Float and java.lang.String are in module java.
public class Product {private SimpleStringProperty name;private SimpleFloatProperty price;
//I need to to a String here from another method and then convert it to a float.
public void setPrice(float price) {this.price.set(price);}}
I need to use SimpleFloatProperty because I use TableVIew.