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

8th Mar 2019, 10:44 AM
Peter
Peter - avatar
4 Réponses
+ 2
float("93.7")
9th Mar 2019, 4:17 AM
Tibor Santa
Tibor Santa - avatar
+ 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!
9th Mar 2019, 9:51 AM
Tibor Santa
Tibor Santa - avatar
+ 1
Thanks for taking the time to give me so much information! I appreciate it!
9th Mar 2019, 10:34 AM
Peter
Peter - avatar
0
Please could you elaborate, Tibor, and tell me why that is that answer? Thanks!
9th Mar 2019, 9:36 AM
Peter
Peter - avatar