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

Display correct number of decimals at runtime

Status
Not open for further replies.

OnDOT

Programmer
Jul 20, 2001
11
SG
hi,

I would like to display a number field to the correct number of decimals at runtime.

e.g
1.0000 as 1
0.8750 as 0.875
0.5000 as 0.5
2.5000 as 2.5

how can I do that?

Cheers,
Eric Lye
 
Hi Eric,
All you have to do is to right click on the field the numbers appear in and choose "Format field" and choose the
numbers tab and from here you can specify how many caracters appears after the decimal.








Peace
Jason
 
Hi Moonmouth,

thanks for your reply, but this is not what I need.

I know that we can format number fields through the format field options. What I need is to be able to set the number field dynamically to the correct number of decimal place for each record retrieved.

e.g.

Rcd no. field value desired display
------- ----------- ---------------
1 1.500 1.5
2 0.875 0.875
3 0.250 0.25

Regards,
Eric Lye
 
You will have to format it as a string, as this is not a standard numeric format.
The following would do it for a number in the format of 9999.9999. It simply checks the last character of the decimal portion of the number, and if it is zero, checks the one prior etc. If it runs into a non zero character in the decimal portion of the number, it displays the string up to that point. There may be a simpler solution...
Code:
Local StringVar TempStr := ToText({Orders.Order Amount}, "####.0000") ;
If TempStr[-1] <> '0' then
  TempStr
Else If TempStr[-2] <> '0' then
  TempStr[1 to -2]
Else If TempStr[-3] <> '0' then
  TempStr[1 to -3]
Else If TempStr[-4] <> '0' then
  TempStr[1 to -4]
Else
  TempStr[1 to 4]
Malcolm Wynden
Authorized Crystal Engineer
malcolm@wynden.net
 
Thanks Malcolm.

This is very helpful.
 
Hi OnDot

If you want a general formula to achieve the same thing...

local numbervar p1;
local numbervar p2;
local stringvar s1
s1 := totext({numfield},&quot;0.000000000000000&quot;);
p1 := instr(s1,&quot;.&quot;);
p2 := instr(s1+&quot;000000000000000&quot;,&quot;000000000000000&quot;);
if (p2 = p1+1) then s1 := left(s1,p1-1)
else s1 := left(s1,p2-1);
s1


This should work with anything provided it does not have more than 15 decimal places !!

Geoff

(PS. I have checked it !!)
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top