Search
Search

# Display 4 decimals only and remove trailing zeros

Hello,

I have CR XI used in .net windows application.

I need a formula that will display a decimal value with minimum 2 decimals and 4 decimals characters (if needed) and will remove all trailing zeros.

Examples:

100 -> 100.00

100.1234 -> 100.1234

100.1200 -> 100.12

Thanks

Edited by: mtlmonk on Apr 27, 2009 7:22 PM

##### Guest replied

Try creating a formula in the report like this

This formula works upto 10 decimal places.

```local NumberVar n := {Decimal Field}
NumberVar Zeros;
StringVar DecimalPortion;

DecimalPortion := ToText (n - (Truncate (n)), 10);

if DecimalPortion[12] &lt;&gt; "0" then Zeros := 10
else if DecimalPortion[11] &lt;&gt; "0" then Zeros := 9
else if DecimalPortion[10] &lt;&gt; "0" then Zeros := 8
else if DecimalPortion[9] &lt;&gt; "0" then Zeros := 7
else if DecimalPortion[8] &lt;&gt; "0" then Zeros := 6
else if DecimalPortion[7] &lt;&gt; "0" then Zeros := 5
else if DecimalPortion[6] &lt;&gt; "0" then Zeros := 4
else if DecimalPortion[5] &lt;&gt; "0" then Zeros := 3
else if DecimalPortion[4] &lt;&gt; "0" then Zeros := 2
else if DecimalPortion[3] &lt;&gt; "0" then Zeros := 1
else Zeros := 0;

if Zeros>2 then
totext(n,Zeros)
else
totext(n,2)```

Hope this helps!

Raghavendra

0 View this answer in context
View more on this topic or