iOS Question Problem with numbers

tufanv

Expert
Licensed User
Longtime User
Hello

I am having a problem i cant find the reason. First pic shows my code and logs according to code:
kwd1 is : 9.5379 in the logs
kwd2 is : 9.6627 in the logs
kwd is : something strange

Question 1 : it must be 9,6003 according to calculations. why is it an interesitng number ?

2cd8x3l.png



As you can see in the in the below pic i am using kwd2 to add to tableview. in the logs in the above pic kwd2 is : 9.6627
Now please check my code in below pic and later check what my app shows for kwd2 in release in third pic:

71h9h5.png

307x06b.png


Altough the kwd2 is 9.6627 it shows something very different in release mode.

Can you help me about it. what am i missing here ?
 

tufanv

Expert
Licensed User
Longtime User
You should post the code with 'code' tags instead of a screenshot. This way we can copy it and post a modified version.

Either use NumberFormat or use Log($"KWD: $1.2{kwd1}"$).
Sorry,
What is the reason for it i want to learn =) while the logs shows good , with release it shows another thing.
Also for example i use:
B4X:
 kwd=NumberFormat((((txtkod.Text.SubString2(bas,bitir))+(txtkod.Text.SubString2(bas2,bitir2)))/2),0,5)

numberformat here but it still shows many fractions altough it must show five
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Also for kwd value i changed the code with numberformat everywhere :
B4X:
  kwd=NumberFormat((((NumberFormat(txtkod.Text.SubString2(bas,bitir),0,4))+(NumberFormat(txtkod.Text.SubString2(bas2,bitir2),0,4)))/2),0,5)

but it is still giving : KWD: 9.600300000000001
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What is the reason for it i want to learn =)
The proper way to convert a floating number to string is with NumberFormat (or the smart string literal). Otherwise any small rounding error can cause the number to show with many fraction digits.

Note that the code you posted in post #4 is incorrect.

You only need to use NumberFormat in the place where you convert the number to string.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
so i must do the calculations and finish them an at the end i must use numberformat right ?
The proper way to convert a floating number to string is with NumberFormat (or the smart string literal). Otherwise any small rounding error can cause the number to show with many fraction digits.

Note that the code you posted in post #4 is incorrect.

You only need to use NumberFormat in the place where you convert the number to string.
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
I am still having problem at this. I have some doubles at my databae with at most 2 decimal points like 403.45

When i use httpjob and retrieve them and add them to table with numberformat(m.get("number"),0,2) i get too many decimal points like :

72.9300000000000000001

How can i fix this = I want to use numberformat to round them to 2 decimal points.

edit: only solution i oculd find is i declared the numbers as string instead of double
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You are not using NumberFormat correctly. You should use it whenever you want to show the number to the user.
I am still having problems with these. I use numberformat2 when I want to show the text to user ( at the end )

my code is :

B4X:
jpy=1/m.Get("last")*100
                    fiyat(4).Text=NumberFormat2(jpy,0,5,5,False) & "  TRY"

this still gives me many fractions , more than 5 altough i set it to maximum 5 with numberformat2 . Am I using it wrong again ?

(edit : also if the m.get("last") is 3.4545 lets say, numberformat2 with minimum fractions 5 does not give me 3.45450 but stil 3.4545 how can i fix that too )
 
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Please reproduce it in a small project and upload it.
I am still having this problem after many months. I can't reproduce it for a small project as numbers are constantly changing every second. I can't find the solution. As you said , I am only using the numberformat when converting my double to string with:

B4X:
lblbtcprofit(sayi).Text="+ % " &NumberFormat(((currentprice-averagecost)/averagecost*100),1,1)

and someitmes when numbers change I get %88.69999999999999...

using the same code with log( it shows : %88.7 . it is really interesting and bothering.
 
Last edited:
Upvote 0

tufanv

Expert
Licensed User
Longtime User
You can check the string length and do something if it is longer than you expected.
I am checking them but they are randomly happening like for example I type 0.00004534 into textbox and add it to keyvaluestore , it is recorded as 0.00004534000000000001 or sometimes 0.00004534999999999999 it is really hard to check this for every entry and fix it . if it was a standard number everytime i could replace it with null but it changes every time it also messes up the cost calculations. This only happens at number with big fractions like 0.00003234 . cant find a proper way to fix this . Do you have a suggestion ?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

tufanv

Expert
Licensed User
Longtime User
Upvote 0
Top