Converting strings to integers not working past the decimal point

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By TheMessenger

I am trying to convert a string to an integer. No matter what I try it doesn’t keep the numbers past the decimal point.

The way I originally tried to do it was this

var new_str = 1.4
var new_int = int(new_str)
print(new_int)

I expected for the output to be “1.4”, however it always outputs “1”

:bust_in_silhouette: Reply From: kidscancode

“Integer” means “whole number”. 1 is an integer, 1.4 is not.

Second, you’re not using a string at all.

var new_str = 1.4

creates a float variable with a value of 1.4.

var new_str = "1.4"

would create a string.

If all you want is 1.4 then you already have that.

If you do have a string such as "1.4" and you want to convert to a float, then you should use float() instead of int().

2 Likes