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

Removing numeric trailing zeros when not required 4

Status
Not open for further replies.

Steve-vfp9user

Programmer
Feb 5, 2013
337
GB
Hello

I have a field on a report called: QTY

The field is a N,7,3

On my report the field is displayed as STR((invprodrecs.qty),7,3) which is fine when the figure is 1.250 or 1.500 etc.

I have changed this to STR(invprodrecs.qty) which works fine if the number is 1, 2, 15 etc but will cut off the .250 or .500 if it exists in the figure.

The help file is once again sparse so my question is, what is the best way to add the field to my report so the trailing zeros will not show if the number is whole (e.g. 1, 4, 11 etc) but show if its 1.250, 1.500 etc.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
This will do it. There's probably a better way.

Code:
* Add to your report field:
remove_trailing_zeros(invprodrecs.qty, 7, 3)

And then in your stored procedures or utility.prg file add this function:

Code:
FUNCTION remove_trailing_zeros
LPARAMETERS tnVal, tnWidth, tnDecimals
RETURN IIF(RIGHT(RTRIM(STR(tnVal, tnWidth, tnDecimals), 1, "0"), 1) = ".", STR(tnVal, tnWidth - tnDecimals - 1, 0), STR(tnVal, tnWidth, tnDecimals))

And some test code:

Code:
? remove_trailing_zeros(234.567, 7, 3)
? remove_trailing_zeros(234.56, 7, 3)
? remove_trailing_zeros(234.5, 7, 3)
? remove_trailing_zeros(234, 7, 3)

--
Rick C. Hodgin
 
Steve,

for the examples you give TRANSFORM() should do the job.

transform(123.000) gives "123"
transform(123.456) gives "123.456"
transform(123.450) gives "123.450"

hth

n
 
What do you get with STR(1.5)? I get 2. Why did you remove the parameters for the formatting of length and decimals?

As Nigel said TRANSFORM() gives what you want. Try a few cases such as
Code:
? Transform(CAST(1 as N(7,3)))
? Transform(CAST(1.5 as N(7,3)))

This prints 1 and 1.500

Chriss
 
Mike,

RTRIM...,'0') is a though I also had , but Steve wanted to have 1.250, 1.500 for such values, only no decimals when they are .000
What you can do is STRTRAN(STR(qty,7,3),'.000','') which also removes the decimal point with the trailing 000.

I wonder about wheth4r this what's better readable output in a list like this:

1.000
1.250
2.000
2.500

or

1
1.250
2
2.500

But perhaps it's a matter of taste.

On an order form or bill I'd make it depend on items you can only have whole quantities like apples vs units like oz or lb. So I'd format 1 to 1.000 when it's about the unit lb, for example.

Chriss
 
perhaps it's a matter of taste.

Personally, I would always choose to have exacty the same number of post-decimal digits within the column, even if this means trailing zeroes. I think it looks neater and is also more conventional.

That doesn't detract from any of the solutions offered here, of course.

Mike


__________________________________
Mike Lewis (Edinburgh, Scotland)

Visual FoxPro articles, tips and downloads
 
I appreciate all your replies.

To resolve my question I used strtran(str(invprodrecs.qty,7,3),'.000') which turned 1.000 into 1 and where I had 1.250 it now shows 1.250

As always, I appreciate the time taken by this forums users to reply.

Stay safe.

Thank you

Steve Williams
VFP9, SP2, Windows 10
 
Note: Using TRANSFORM() won't keep alignment. It will return "1" and "11" and "111" which are three different lengths without the leading spaces.

Probably not an issue.

Using STRTRAN() will also work, but you must code it each place you want to use it. The syntax is specific. If you passed in the alias and field name, or only the field name and assumed current alias, you could derive width and decimals and it would require no future code changes when the width or precision changes.

Some things to consider for long-term maintenance.

--
Rick C. Hodgin
 
Well,

Code:
? LEN(STRTRAN(STR(1,7,3),'.000',''))
? LEN(STRTRAN(STR(11,7,3),'.000',''))
? LEN(STRTRAN(STR(111,7,3),'.000',''))

All are 3 characters long. The 4 characters for '.000' are removed.
What get's wrong is 1111. It becomes 1111.00 as 7 digit places are not enough for 3 decimal places. But otherwise STR returns fixed length right delimited strings, even without a parameter.

Chriss
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top