Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Different no. of decimals for each record 1

Status
Not open for further replies.

stillsteam

Programmer
Apr 2, 2004
52
SE
Hello
I use CR 8.5 and a SQL-databas.
In my report I have a formula field that calculates an amount and it is a currency-field.
For some records I need to have 4 decimals printed but for others I don´t need decimals at all.
The field is in a group
Ex.
I have in my field these figures
1,2350
10,0000
10 000,0000
0,1000

and I want to get this in my report.
1,235
10
10 000
0,1

I have tried different ways to get it but I am lost.
I haven´t been working with this for too long, so if you need more info just tell me.
I´ve search here but haven´t been able to find it or maybe I´m not good enough at this :)
Please help me!
 
You will need to convert this number to a string and rebuild using your logic i.e.

Code:
WhilePrintingRecords;

//Convert The Number to a string with 4 decimal places
StringVar strNumber := cstr({MyTable.NumberField},4); 
//Find the position of the decimal point
NumberVar intDecPoint := InStr (strNumber,'.'); 
//get the integer part of the number
StringVar strInteger := Left(strNumber,intDecPoint - 1); 
//get the decimal part of the number
StringVar strDecimal := Mid(strNumber,intDecPoint + 1, 4); 
//Temporary variable for use later
StringVar strTemp := ''; 
//counter
NumberVar intCount; 

//Loop backwards through the decimal string
//and remove trailing zeros 
For intCount := 4 to 1
Step -1
Do(if strTemp = '' Then
        (if strDecimal[intCount] <> '0' Then strTemp := strTemp & strDecimal[intCount])
    Else
        strTemp := strTemp & strDecimal[intCount]
);
    
//Output final string
(If strTemp <> '' Then
    strTemp := strInteger & '.' & StrReverse(strtemp)
Else
    strTemp := strInteger);

strTemp

This should get you started

Gary Parker
Systems Support Analyst
Manchester, England
 
You can use conditional formatting to get different decimals for different records without converting the field to a string, but you haven't told us what conditions would result in the different decimals. Right click on the field->format field->number->customized->decimals->x+2 and enter:

if <your condition here> then 4 else 0

-LB
 
Hello
Thank you for your reply.

I think I need more help cause I get an error that say "Stringlength is less than 0 or not an integer"
I put the code in a new formula and put it in the detailed section.

The reports is a list of products where I have calculated the price of the products.

They are grouped in what kind of product it is and some of them have a low price (like 0,0345) other have a much higher price (like 10, 100 or even 10 000). SO I don´t want to display 10 000,0000

I have to have 4 decimals for some products but for others I don´t need any at all.

Hope this made it easier to understand
Thanx in advance
 
Try my earlier suggestion using a formula like:

if {table.price} > 9999 then 0 else 4

Substitute your own number for "9999".

-LB
 
I know what you mean but I don´t know how to write the formula.


If the price is 10 000,0001 I want it to come out as 10 000,0001, if the price is 12,1250 I want it to come out as 12,125

I want to get rid of all trailing zeros.

How should I write the formula to achive this, is it possible

Thanx -JJ

 
JJ

In your examples is the comma (, character) being used as a thousand seperator or a decimal point.

is 10 000,0001 = ten thousand point 0001
and 12,1250 = twelve point 125

If this is so then amend my formula to use the , instead of .

i.e.

//Find the position of the decimal point
NumberVar intDecPoint := InStr (strNumber,',');

and

strTemp := strInteger & ',' & StrReverse(strtemp)

Gary Parker
Systems Support Analyst
Manchester, England
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top