By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,778 Members | 1,318 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,778 IT Pros & Developers. It's quick & easy.

Set decimal places

P: n/a
Hi, I need to truncate a number to two decimal places without rounding.
All the functions i;ve tried tend to round up the numbers. Also i cant
use any string functions to limit the size because the size can vary.
example, 34.56998 must be set to 34.56
thanx
any help is greatly appreciated
btw this is using asp vbscript
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Dean G wrote on 07 nov 2004 in microsoft.public.inetserver.asp.general:
Hi, I need to truncate a number to two decimal places without rounding.
All the functions i;ve tried tend to round up the numbers. Also i cant
use any string functions to limit the size because the size can vary.
example, 34.56998 must be set to 34.56
thanx
any help is greatly appreciated
btw this is using asp vbscript


Use a regex replace:

function round2(x)
if instr(x,".")=0 then x = x & "."
Set re = New RegExp
re.Pattern = "(\d*(\.\d{1,2})?)\d*"
round2 = re.Replace(x & "00", "$1")
end function

--
Evertjan.
The Netherlands.
(Please change the x'es to dots in my emailaddress,
but let us keep the discussions in the newsgroup)

Jul 21 '05 #2

P: n/a
Thanx Evertjan,
Thats pretty clever i can see why i didnt think of that, it did the
trick perfectly.
Thanx for the help
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
Jul 21 '05 #3

P: n/a
Jos
Dean G wrote:
Hi, I need to truncate a number to two decimal places without
rounding. All the functions i;ve tried tend to round up the numbers.
Also i cant use any string functions to limit the size because the
size can vary. example, 34.56998 must be set to 34.56
thanx
any help is greatly appreciated
btw this is using asp vbscript
*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!


dTruncated = (Int(d*100))/100
or
dTruncated = (Fix(d*100))/100

--

Jos
Jul 21 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.