php - Why do two seemingly identical variables produce two different results? -
I am trying to write some code to add cricket overs. Number 1 in the numbers is 0.1, 0.2, 0.3, 0.4, 0.5, 1.0, 1.1 etc. and therefore does not work by adding decimal in a standard way.
I have followed the advice given about this thread - to make a real conversion, and when I put a random number in the equation, it gives it expected though, Using a number from the database, it does not return the same thing.
I have tried to create some example code and output to describe the problem. In this example, the variable is equal to $ xb 1.2, as you will see in the output.
Code:
"Var type -" echo gettype ($ xb) 'value =' $ Xb '& lt; Br / & gt; '; "Type Type -" echo gettype (1.2) 'value = 1.2 & lt; Br / & gt; '; $ Over = floor ((1.2 * 10) / 6); $ Ball = (1.2 * 10) - ($ over * 6); $ X_overs = Floor (($ xb * 10) / 6); $ X_ball = ($ xb * 10) - ($ x_overs * 6); Echo "Total Over =" $ Over ". . $ Balls "& lt; br / & gt;"; Echo "Total X Over =" $ X_overs '.' . $ X_ ball "
";
Output:
Type - Double Value = 1.2 Var Type - Double Value = 1.2 Total Over = 2.0 Total X Over = 1.6
I am convinced that I should be a fool by default or have some basic misconception, but I'm staring for it for an hour and can not see it. Can anyone make me out of my misery?
I suggest two formats, one for display and one for processing. You will need a routine to convert between the two formats.
In the processing format, I suggest increasing units to 6 and adding it to tenth position. So "1.0" would be 6, "1.1" would be 7, and so on. In this way, you can easily manipulate numbers in the processing format, add them as desired and reduce them. You can manipulate these numbers in the whole number, so floating point golfing has no chance to mess you up.
To move from the processing format to display the format, divide by six using the integer division. The result is the location of the units and the remaining is the tenth place. So divided by 8 is 6 1 remaining 2. Therefore, an internal value of 8 corresponds to a display value of "1.2".
Comments
Post a Comment