From another thread I found how to use format with integer.ToText, and after reading about the use of 1-9 for rounding purposes I did some tests and I’m having different results on values I think is not consistent.
Let’s say that you want to round a value to 5 cents, because there are no 1 cent coins in your country (example), so I’m using this:
label4.Text = value2.ToText(xojo.core.locale.raw, "#,##0.05")
That way if I have 0.025 or less, the number shown is 0.00, if the number is 0.0251 it shows 0.05
The problem is with 0.075, it shows 0.10 and I expected 0.05.
Edit: 0.025 should be 0.05, 0.075 is correct at 0.10 (see my note on next post)
Then I changed the format value to #,##0.03, the results I get:
0.015 - 0.00 <-- should be 0.03
0.045 - 0.06
0.075 - 0.06 <-- should be 0.09
0.105 - 0.12
0.135 - 0.12 <-- should be 0.15
and so on.
I said, ok, double is not exact, maybe some decimal at the end of those values. So I used Integer and #,#50. The results I get:
25 - 00 <-- should be 50
75 - 100
125 - 100 <-- should be 150
175 - 200
Now #,#30:
15 - 00 <-- should be 30
45 - 60
75 - 60 <-- should be 90
105 - 120
135 - 120 <-- should be 150
NOTE: the information below is working correctly, I didn’t understand the behavior when I first wrote it
When I did the tests for Integer I left the double format as #,##0.03 and I found something strange too:
15 - 15.00
25 - 24.99 <-- should be 25
30 - 30.00
45 - 45.00
50 - 50.01 <-- should be 50
60 - 60
75 - 75
90 - 90
100 - 99.99 <-- should be 100
*Edit: I think the issue is that 25, 50 and 100 are not divisible by 3, so I guess this is working right, 25 in format #,##0.03 should be 24.99
More interesting is when you format an integer with decimals and both with rounding, like #,#50.05:
25 - 00
26 - 50.05
75 - 50.05
76 - 100.10
126 - 150.15
BTW, double with same format, get the same results
Now double with #,#50.05:
25.025 - 00
25.026 - 50.05
49.0 - 50.05
I know that you should not use #,##0.05 if you using integers, but I think the program should ignore the .05 if the number has no decimals.
I don’t know if #,#50.05 is a valid rounding format. I can’t find this information on the unicode standard. I can’t think of a situation when you need to round like that. I guess I get carried away testing
Edit: after reading, testing and understanding a little bit more I know how the #,#50.05 format works:
result = round (value / rounding) * rounding
so for 25, round ( 25 / 50.05) * 50.05 = round ( 0.4995 ) * 50.05 = 0 * 50.05 = 0
so for 25.026, round (25.026 / 50.05) * 50.05 = round ( 0.50000199) * 50.05 = 1 * 50.05 = 50.05