Print Page | Close Window

count decimals

Printed From: Crystal Reports Book
Category: Crystal Reports 9 through 2020
Forum Name: Technical Questions
Forum Discription: Formulas, charting data, Crystal syntax, etc.
URL: http://www.crystalreportsbook.com/forum/forum_posts.asp?TID=16794
Printed Date: 02 May 2024 at 12:58pm


Topic: count decimals
Posted By: Athlon
Subject: count decimals
Date 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



Replies:
Posted By: hilfy
Date 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


-------------
Proviti, Data & Analytics Practice
http://www.protiviti.com/US-en/data-management-advanced-analytics - www.protiviti.com/US-en/data-management-advanced-analytics


Posted By: Athlon
Date 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


Posted By: yggdrasil
Date 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

 



Posted By: rkrowland
Date 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.


Posted By: Athlon
Date 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 Clap
 
TT
 
 


Posted By: rkrowland
Date 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.


Posted By: Athlon
Date Posted: 21 Jun 2012 at 9:32am
Thanks again
 
TT



Print Page | Close Window