Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

DarkBASIC Professional Discussion / converting a real number to an integer

Author
Message
Dracula
19
Years of Service
User Offline
Joined: 7th Aug 2006
Location: DBP Recreation of Castle Csejthe
Posted: 2nd Feb 2012 20:59
I am trying to obtain a percentage of a number that will end up something like 33.058347563498... How do I convert this to an integer?

This is the code I have tried, but it doesn't work:



Can someone help me please? I tried using WORD, DWORD, and # but the variable scaleval keeps coming back 0

Thanks for the assistance!

Jason
Pincho Paxton
23
Years of Service
User Offline
Joined: 8th Dec 2002
Location:
Posted: 2nd Feb 2012 21:12
The int command...

print int(33.058347563498)

Dracula
19
Years of Service
User Offline
Joined: 7th Aug 2006
Location: DBP Recreation of Castle Csejthe
Posted: 2nd Feb 2012 21:16
It seems the problem is in the math sentence: (1024/3088)*100

Why won't this calculate? When I use numbers that yield whole numbers, it works fine, but I get a 0 when there is a decimal...
Brendy boy
20
Years of Service
User Offline
Joined: 17th Jul 2005
Location: Croatia
Posted: 2nd Feb 2012 21:24
Quote: "It seems the problem is in the math sentence: (1024/3088)*100

Why won't this calculate?"

if you use integer in your calculations the result will be integer too (rounded up to nearest integer)

you have to do it like this: (1024.0/3088.0)*100.0

In your first post you should use floats and then convert result at the end to integer with INT command

Green Gandalf
VIP Member
21
Years of Service
User Offline
Joined: 3rd Jan 2005
Playing: Malevolence:Sword of Ahkranox, Skyrim, Civ6.
Posted: 3rd Feb 2012 01:55
Quote: "you have to do it like this: (1024.0/3088.0)*100.0"


or



or even



The problem with the original is that the bit in brackets is performed first so (1024/3088) is evaluated using integer arithmetic and hence evaluates to zero. Multiplying that result by 100 will of course give just zero.

Login to post a reply

Server time is: 2026-07-10 23:33:21
Your offset time is: 2026-07-10 23:33:21