0 votes

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"

Godot version 3.4
in Engine by (12 points)

1 Answer

+1 vote

"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().

by (22,067 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.