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!

setting decimal range 1

Status
Not open for further replies.

jstepun

Programmer
Oct 18, 2000
2
US
I need to set the decimal range and round the last digit of a number here is an example:

1.010899

I can only have 4 numbers after the decimal, so I need the number to be 1.0109 [sig][/sig]
 
Okay, in order to put the number in the right decimal place you would write:


PRINT USING "###.####";

Adding another # sign after the decimal adds another decimal place, and vise versa on
the other side. Then you would type the number.

NOTE: This will not round the number, it will just take off the extra digets
I.E. 1.849 = 1.84
 
One way is
a= 1.010899:pRINT USING #.####";a

Another way that uses your brain a little more is
to do your own rounding.
a = 1.010899
n = 4 'number of digits to have after decimal
PRINT (INT(a * 10 ^ (n + 1) + 5) \ 10) / 10 ^ n

 
Print using does not round the variable, it only display the variable in a rounded fashion. Your variable 'a' will still = 1.010899. To round the variable to 4 decimals you can write a function.

DEF FNNUMFIX#(numfix#) = FIX((numfix# * 10000 + SGN(numfix#)*.5) / 10000

You call it like this

a = FNNUMFIX#(a)

a will be 1.0109 [sig]<p>David Paulson<br><a href=mailto: > </a><br><a href= > </a><br> [/sig]
 
David, good catch there. Indeed it does not round the number in the variable, and my code will not work for negative numbers! Thanks for the bugfix!
 
thank you all. you have been a big help. [sig][/sig]
 
Jstepun, I know you are new to the site so you may have missed the link at the bottom left of each post. It will say something like:

Was this post helpful?
Vote dpaulson Tipmaster!

Everybody here believes a simple &quot;thank you&quot; is more than enough reward for helping out... but sometimes we get an extra tickle when somebody clicks that little link.

Hope you enjoy Tek-Tips as much as I have!

[sig]<p> <br><a href=mailto: > </a><br><a href= plain black box</a><br><i>"I'm all right now, Dave. Really, I am."<br>
<b>HAL, </i><u>2001 a Space Odyssey</u></b><br>
<br>
[/sig]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top