Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

I need to somehow format a Qty field to only show significant digits past the decimal point, trimming any trailing zeros.

(i.e. 4.567 would display 4.567, 4.560 would display 4.56, 4.500 would display 4.5)

thanks!

(i.e. 4.567 would display 4.567, 4.560 would display 4.56, 4.500 would display 4.5)

thanks!

Do more with

EXPERT OFFICE^{®} is a registered trademark of EXPERTS EXCHANGE^{®}

```
4.567
4.56
4.5
```

Assuming that that's not a problem, you can give the following a try:

Go into the field format, click Customize on the Number tab, click the formula (X+2) button beside Decimals and enter the following formula:

Local NumberVar places;

places := 0;

while Truncate (CurrentFieldValue * 10 ^ places) <> CurrentFieldValue * 10 ^ places do

places := places + 1;

places

CurrentFieldValue is a special CR function used to reference the value in the field in a formatting formula. You could use the actual field name instead. CurrentFieldValue is particularly handy if you want to use the same format formula for multiple fields. Then you don't have to change the field name in each formula.

What that formula does is multiply the field by increasing powers of 10, until the result without decimals (Truncate) matches the result with decimals. For example, with 4.56, you'd get:

```
Places Result
0 4 <> 4.56 (keep going)
1 45 <> 45.6 (keep going)
2 456 = 456 (stop)
```

So places ends up set to 2, and you get 2 decimal places in the field format (ie. 4.56).

You'd want to enter the same formula for the Rounding part of the field format, so that CR will also round the field to the correct number of decimals.

James

Is it a field on the report or is it in a formula that produces a string?

mlmcc

"The total is : " & CStr({MyValue})

James suggestion should work if you are just displaying the value or in a cross tab.

mlmcc

What isn't working about the formula?

You use that in the CUSTOM FORMAT of a number control the decimal places shown.

You might also have to change the ROUNDING so that it rounds with more significant digits than you might need like at 0.0000000001

If this is just for display then you could convert it to a regular formula.

```
Local NumberVar places;
places := 0;
while Truncate (CurrentFieldValue * 10 ^ places) <> CurrentFieldValue * 10 ^ places do
places := places + 1;
CStr(CurrentFieldValue , places)
```

mlmcc

what my customer wants is for a variable number of digits after the decimal with no zeros.That is _exactly_ what my suggestion should do. What it's doing is changing the number of decimal places in the field format, depending on how many significant digits there are.

James

Can you post your formula? I used the formula that I posted on a field and it worked fine.

James

Just to be clear, here's my formula again (the same thing I posted earlier):

```
Local NumberVar places;
places := 0;
while Truncate (CurrentFieldValue * 10 ^ places) <> CurrentFieldValue * 10 ^ places do
places := places + 1;
places
```

Also, as I mentioned in my earlier post, you should enter the same formula for Rounding in the field format.

James

mlmcc

## Premium Content

You need an Expert Office subscription to comment.Start Free Trial