Set decimal places

D

Dean G

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
 
E

Evertjan.

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
 
D

Dean G

Thanx Evertjan,
Thats pretty clever i can see why i didnt think of that, it did the
trick perfectly.
Thanx for the help
 
J

Jos

Dean said:
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

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,767
Messages
2,569,570
Members
45,045
Latest member
DRCM

Latest Threads

Top