0 votes

I'm trying to get the decimal part only of my number by trying to divide it to 1 and getting the remainder.

This is how I tried it:

answer = somefloat%1

However it gives me an error saying Invalid operands 'float' and 'int' in operator '%'.

So I also tried turning '1' into float:

answer = somefloat%1.0

Then it gave me this Invalid operands 'float' and 'float' in operator '%'.

Can someone please tell me any other solutions for this?

in Engine by (31 points)
edited by

You're not looking for the reciprocal. The reciprocal of a number is the number that, when multiplied by that number, will give 1.

Oops.. Yeah.. Sorry. I was meant to say remainder, not reciprocal. lol

Will edit now.. :P

2 Answers

+3 votes
Best answer
by (28,791 points)
selected by

Ooh.. I didn't know that existed.. Thanks!

For those of us in the future, the function is really fmod

0 votes

Nevermind.. I got the answer. Though I don't know if this is the best way to do it.

Apparently '%' operator can't deal with floats so to get the decimal part only I used this:

answer = somefloat - int(somefloat)

By using int(), it automatically gets rid of the decimal part so when you subtract it to the ones with a decimal value, you could get rid of the integer part.

by (31 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 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 webmaster@godotengine.org with your username.