0
My course is asking me to convert a variable containing string "93.7" and another to a decimal. How is this done? Thanks guys.
Feeling confused
4 ответов
+ 2
float("93.7")
+ 2
You might also want to indicate which programming language you are asking about. I only assumed python.
You cannot do computations with a string, even if it contains a numeric value. You need to convert it to numeric type. This is also called typecasting. Some programming languages don't even allow to mix integer and decimal types. Integers are whole numbers (without decimal points). A number with decimals is called a float, or in some languages you can add more precision (digits) with a type 'double'.
Example:
x = "93.7" # this is a string
y = x / 2 # this won't work
y = float(x) / 2 # typecasting!
+ 1
Thanks for taking the time to give me so much information! I appreciate it!
0
Please could you elaborate, Tibor, and tell me why that is that answer?
Thanks!