Does anyone know of any scripts/hacks to do percentages?
I'm trying to find the percentage of rotten apples in a barrel.
200 apples
10 are bad
percentage: (10/200) x 100 = 5%
The result in Hamster is 0, I guess because I'm using integers.
A side note: When searching the wiki I noticed an example that returns a zero value if the max stat is less than 100.
http://rpg.hamsterrepublic.com/ohrrpgce ... ripting%3F
I'm not looking forward to writing a huge long division script
Percentages / floating point HOWTO?
Moderators: marionline, SDHawk
-
- Slime
- Posts: 20
- Joined: Mon Feb 13, 2017 10:28 am
- Location: Moscow
You can circumvent the integer issue by adjusting the formula
if you need the part behind the decimal point you could multiply by 100 to get two decimal places.
Example:
So you could treat the last two digits as decimal places.
If you convert it into a string put a decimal point in front of the last two digits.
Code: Select all
percentage: (10/200) x 100 = 5%
is the same as
percentage: (10 x 100) / 200 = 5%
Example:
Code: Select all
percentage: (10 x 100) / 200 = 5[,00000]
...
percentage: (10 x 100 x 100) / 200 = 5[00]
If you convert it into a string put a decimal point in front of the last two digits.
Last edited by Wendigo on Mon Mar 13, 2017 10:17 am, edited 1 time in total.
Thanks, that was a pretty major error on that wiki page! I've fixed it.
You can nearly always work around the lack of floating point by scaling everything up by a constant factor or by being careful of the order in which you perform operations.
For example, in the Sidescroller 101 scripts, positions and velocities are measured in tenths of a pixel everywhere. They're only converted to pixels at the last moment when calling "put hero".
You can nearly always work around the lack of floating point by scaling everything up by a constant factor or by being careful of the order in which you perform operations.
For example, in the Sidescroller 101 scripts, positions and velocities are measured in tenths of a pixel everywhere. They're only converted to pixels at the last moment when calling "put hero".
Last edited by TMC on Mon Mar 13, 2017 4:05 pm, edited 1 time in total.
-
- Slime
- Posts: 20
- Joined: Mon Feb 13, 2017 10:28 am
- Location: Moscow