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

Rounding down / truncating numbers in ASP 1

Status
Not open for further replies.

xjs

Programmer
Apr 4, 2002
21
US
Does anyone know how to strip a decimal so that a rounded down integer is returned? Would I have to parse text from the number and remove the decimal and everything to the right of it?

It sounds like it should be easy but I can't find what I need on the web. Am I missing something?

Thanks.
 
how's this for a ugly function
Dim var, str
str = 123.32
var = Left(str, instr(str,".")-1)
A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
The Math object has a method round()which will do this for you eg.
var test = 12.3;
offset = Math.round(test);
Response.Write(offset);
 
thank you all for the help, i'll try both ways.
 
How about
Code:
Dim var, str
str = 123.32
var = cint(str)
Thanks,

Gabe
 
Also consider Math.floor() if you are using JScript. Math.round() rounds the number:

2.4 ==> 2
2.5 ==> 3
2.6 ==> 3

whereas Math.floor() truncates the decimal, effectively forcing a round down

2.4 ==> 2
2.5 ==> 2
2.6 ==> 2


 
The vbscript function fix will remove any decimal value from the number.
The differance between using fix and using cInt or Round is that fix doesn't round at all, it only removes decimals from an number:
Code:
Number cInt	Round	Fix
5.333	5	5	5
5.8	6	6	5
-5.333	-5	-5	-5
-5.8	-6	-6	-5
If you really only want the base integer of the number, without rounding, use fix, if you want the closest integer representation (rounding) use cInt or Round.

-Tarwn --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
hmm, repost to look more readable:
Code:
Number   cInt   Round   Fix
5.333     5       5      5
5.8       6       6      5
-5.333   -5      -5     -5
-5.8     -6      -6     -5

That might make a little more sense :) --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
very nice Tarwn. I think out of all of us you get the star on this one. A language that doesn't affect the way you think about programming is not worth knowing.
admin@onpntwebdesigns.com
 
Thanks :)
stickhorse.gif
--- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- --- ---
For my next trick I will pull a hat out of a rabbit (if you think thats bad you should see how the pigeon feels...) :p
 
"Fix" was the fix. Very much appreciated. Thank you, Tarwn... xjs
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top