10.07.2018, 04:37
Quote:
|
He is trying to say don't use strings at all, Use implemented functions to extract the integer and fraction, This "probably, I'm not sure" should be faster than using a string
Explained as much as I can below PHP код:
Having variables is fine, as long as you're using them right, Variables don't take much space or time to make anyways, And sometimes they helps clear things out even if they're unnecessary. an example for the above code with variables: PHP код:
|
Quote:
|
You don't have to use 2 separate arrays to store this.
When you give the player some money like $450, give him 450 * 100 = 45000. Then you'll be storing his money in cents instead of whole dollars. To extract both values (the dollars and cents), you can use it like this: Код:
new money, dollars, cents;
money = 5062; // This is in fact 50 dollars and 62 cents
dollars = money / 100;
cents = money - (dollars * 100);
printf("Money: %i", money);
printf("Dollars: %i", dollars);
printf("Cents: %i", cents);
In this example, the dollars variable will hold "50" (5062 / 100 is essentially 50.62 but with integers, the part behind the comma drops away, leaving you with just the value "50"). Then you take the difference between the whole amount of cents and the rounded down value of dollars multiplied by 100. This will become 5062 - (50 * 100) = 5062 - 5000 = 62. So with only 1 value, you can store both values and separate them easily. The only drawback to this: you are only able to store money up to 21 million dollars instead of 2.1 billion dollars due to having a signed 32-bit integer value and using the last 2 digits to store your cents instead of whole dollars. |


