Author |
Message |
Athlon
Newbie
Joined: 19 Jun 2012
Location: United States
Online Status: Offline
Posts: 7
|
Topic: count decimals Posted: 19 Jun 2012 at 7:45am |
Hello
I'm trying to count the portion of a number after the dot but so far no luck.
For example the field is number with value 10.7523, just like to return 2 since there are 2 digits after the dot
Formula1: instr(totext({table1.price}),”.”) ---à>> it will return 3 which is good
Formula2: mid(totext({table1.price}),{@Formula1}+1) --à>> it returns only 75 and not 7523, not sure why
Formula3: len({@Formula2}) --à>>> it will return 2 since only 2 digits count in formula2, but in itself, this one is not a problem
Thanks
TT
Edited by Athlon - 20 Jun 2012 at 2:20am
|
IP Logged |
|
hilfy
Admin Group
Joined: 20 Nov 2006
Online Status: Offline
Posts: 3701
|
Posted: 19 Jun 2012 at 11:35am |
You're only getting two digits in Formula 2 because the default number format when using ToText is two digits after the decimal. -Dell
|
|
IP Logged |
|
Athlon
Newbie
Joined: 19 Jun 2012
Location: United States
Online Status: Offline
Posts: 7
|
Posted: 20 Jun 2012 at 2:20am |
thanks
I have no choice but using totext, since instr can only work with string.
Would you know of different way of counting the decimal?
thanks
TT
|
IP Logged |
|
yggdrasil
Senior Member
Joined: 19 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 150
|
Posted: 20 Jun 2012 at 3:01am |
totext({field} ,3) will give you 3 decimal places.
if you have Crystal Help installed, search on ToText and it will tell you all the possible parameters
|
IP Logged |
|
rkrowland
Senior Member
Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
|
Posted: 20 Jun 2012 at 3:50am |
As you don't know how many decimals you need to specify for the totext function this is how I'd do it.
numbervar x:= abs({table.field});
stringvar y:= strreverse(totext(x,10,"")); numbervar z:= tonumber(y); numbervar c:= len(totext(z,0,""));
c
Maximum of 10 decimal places allowed by the totext function I'm afraid.
Regards,
Ryan.
Edited by rkrowland - 20 Jun 2012 at 4:00am
|
IP Logged |
|
Athlon
Newbie
Joined: 19 Jun 2012
Location: United States
Online Status: Offline
Posts: 7
|
Posted: 20 Jun 2012 at 9:03am |
Hello there
How I could ever thank you for this? but thank you thousand times!
It works....I would never have have thought of the abs + strreverse
thanks
TT
|
IP Logged |
|
rkrowland
Senior Member
Joined: 20 Dec 2011
Location: England
Online Status: Offline
Posts: 259
|
Posted: 20 Jun 2012 at 9:56pm |
No problem, looking at it again I've seen a possibility of it being incorrect, the below will fix it;
numbervar x:= abs({table.field});
stringvar y:= strreverse(totext(x,10,"")); numbervar z:= truncate(tonumber(y)); numbervar c:= len(totext(z,0,""));
c
An example that could go wrong would be a starting value of 8.999, the entire process would go as follows;
x = 8.999
y = 0000000999.8
z = 999.8
now here's where the error would be, the totext(z,0,"") part of c would return 1000, giving a count of 4, adding the truncate to the formula give a return of 999 which gives us the correct count of 3.
Regards,
Ryan.
Edited by rkrowland - 20 Jun 2012 at 10:03pm
|
IP Logged |
|
Athlon
Newbie
Joined: 19 Jun 2012
Location: United States
Online Status: Offline
Posts: 7
|
Posted: 21 Jun 2012 at 9:32am |
|
IP Logged |
|
|