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 SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

get rid of trailing zeros

Status
Not open for further replies.

beltmanjr

Technical User
Oct 29, 2007
333
0
0
NL
Hi all,
I'm trying to find a way to get rid of trailing zeros (0) from numbers (decimals only), but the number of decimals differs with each record and there is no set number of decimals in the end result.

So I need to get something like:
123.456 -> 123.456
123.450 -> 123.45
123 -> 123
123.45670 -> 123.4567

Is the a nice manner in which to do this?

I was looking at finding out the number of decimals first, convert it totext and do the old fashioned loop. However, I can't determine the length of a number :)

Any ideas? Besides converting it totext with like 100 decimals and then looping :)
 
for each of your displayable number value, use a formula like follows:

totext({your number}, "", 0)

in the formula, the double quotes will eliminate the thousand separator while the 0 will take out the trailing 0s.
 
This results in an error. (bad number format string).
I believe that this is because the second argument should either be 0 or # and the 3rd would indicate number of decimals. In this case 0, which is not what I was after :(
 
Sorry that I messed it up. it should be:

totext({your number value}, 0, "")
 
Please see thread767-1428581.

-LB
 
Neither formula given in that thread would do that what I was trying to accomplish.

The main issue is the unknown number of decimal places.

You could of course just assume a max of say 100 decimals and keep looping determining whether the last digit is 0, if not take the length of your string - 1 and try again, but if something more elegant is available it would be welcomed.

Shame Buck149's solution doesn't work as something as simple like that would be perfect.
 
My suggestion should work. You can't reasonably be expecting 100 decimals, I don't think, and Crystal wouldn't accept that many in any case. The formula also does not test just the last digit. Did you try it?

-LB
 
Code:
if val(right(totext(currentfieldvalue,4),4))=0 then 0 else
if val(right(totext(currentfieldvalue,4),3))=0 then 1 else
if val(right(totext(currentfieldvalue,4),2))=0 then 2 else
if val(right(totext(currentfieldvalue,4),1))=0 then 3 else
4
The formula converts any number to a 4 decimal digit and then checks for 0. As stated before, I dont know how many decimals to expect. If we're talking about some scientific figures I could need like 50 decimals :(

The best would be to be able to determine the number of decimal places, but I cant see a how to do that.
 
Try this:

numbervar dec := 9; //set your maximum number of decimals here
numbervar i;
numbervar j := dec + 1;
numbervar x;

for i := 1 to j do (
if val(right(totext(currentfieldvalue,j,""),i)) = 0 then
x := j - i);
x

-LB
 
Hi IBass,
I have something very similar which I'll publish here next week as it may help others.

I put it down as a function with an optional parameter for number of decimals, a round function, a function that takes care of negative numbers and using a while statement to do exactly what you do with the for statement.

But the problem remains that I'm guessing the number of decimals rather than knowning them. Oh well, guess it's time to give up on the ideal solution.

Many thanks for helping me out! :)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top