Truncating decimals

1. Junior Member
Join Date
Dec 1969
Posts
2

## Truncating decimals

I&#039;m doing some calculating and my nubmers are coming out<BR>3.3333333333333, I want it to display only two digits after the decimal. I don&#039;t want to round tho. Please help. <BR><BR>Thanks!<BR>LindaBEE

2. Senior Member
Join Date
Dec 1969
Posts
2,031

## RE: Truncating decimals

FormatNumber(yourvalue,2)<BR>BTW, check out the VBScript reference at the left...lots more built-in functions there.

3. Senior Member
Join Date
Dec 1969
Posts
2,809

## Actually

I think that rounds the number up. might have to so something <BR>like <BR><BR>dim pos<BR>pos = instr(,intNumber,".")<BR>pos = pos + 2<BR><BR>truncatedNumber = left(intNumber,pos)

4. Senior Member
Join Date
Dec 1969
Posts
96,118

## No rounding?

So if you have 6.66666666 you want it displayed as 6.66 instead of as 6.67?<BR><BR>You&#039;ll have to write your own code for that, I&#039;m afraid.<BR><BR>&#060;%<BR>Function TruncateTwo( num )<BR>&nbsp; &nbsp; Dim temp, sgn<BR>&nbsp; &nbsp; If num &#060; 0 Then sgn = "-" Else sgn = ""<BR>&nbsp; &nbsp; temp = Int( Abs(num) * 100 ) &#039; truncate correct number of digits<BR>&nbsp; &nbsp; If temp &#060; 9 Then <BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = sgn & "0.0" & temp<BR>&nbsp; &nbsp; ElseIf temp &#060; 99 Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = sgn & "0." & temp<BR>&nbsp; &nbsp; Else<BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = sgn & (temp 100) & "." & Right(CStr(temp), 2)<BR>&nbsp; &nbsp; End If<BR>End Function<BR>%&#062;<BR><BR>Untested, but I think that works.<BR><BR>Yes, that *is* a backslash there, not a forward one.<BR><BR>

5. Senior Member
Join Date
Dec 1969
Posts
2,809

## Oops...

take out that first comma...that&#039;s a little better<BR><BR>dim pos<BR>pos = instr(intNumber,".")<BR>pos = pos + 2

6. Senior Member
Join Date
Dec 1969
Posts
96,118

## Hungarian got you again!

If the variable is truly called "intNumber" then it is *supposed* to be holding *only* integers.<BR><BR>SO let&#039;s say:<BR><BR>intNumber = 7<BR>pos = InStr( intNumber, "." ) <BR>&nbsp; &nbsp; &#039; Hmmm...so since there is no decimal point, pos is zero<BR>pos = pos + 2 &#039; so now pos is 2<BR>left(intNumber,pos) &#039; left("7",2)<BR><BR>You know, I hate it, but that works. Only because LEFT is forgiving: If you give it a number too big, it just grabs everything.<BR><BR>Grumble.<BR><BR>But, still, why did you use intNumber when the original example of 3.33333333333 clearly calls for dblNumber???<BR><BR>

7. Senior Member
Join Date
Dec 1969
Posts
2,809

## Grrr

You&#039;re just mad cuz my lousy solution actually works...:)<BR><BR>As for intNumber, I have the really NASTY habit of naming all my numeric values ints, regardless of types. It hasn&#039;t bitten me yet, but I&#039;m sure it will in the future. I submit humbly to the intense bashing I&#039;m about to receive...

8. Senior Member
Join Date
Dec 1969
Posts
96,118

## You're right...

It&#039;s a heluva lot simpler than mine.<BR><BR>At first, I thought it wouldn&#039;t work with small integers, but then I remembered that LEFT is forgiving.<BR><BR>I will only point out that it does *NOT* produce two digits after the decimal point in *ALL* cases, so if you were using this with money (for example) it doesn&#039;t really do the job.<BR><BR>

9. Senior Member
Join Date
Dec 1969
Posts
2,809

## Not sure I understand

Why wouldn&#039;t this work with money? Ahhh, is it because it would put .00 at the end of it? *Technically* that&#039;s the correct 2 digits after the decimal point, isn&#039;t it?<BR>

10. Senior Member
Join Date
Dec 1969
Posts
96,118

## Nope! *Try it*!

Go ahead. Give your code *any* integer value for "intNumber".<BR><BR>Where are you going to get the ".00" from???<BR><BR>Oh...just realized. You wrote:<BR><BR>&#062; Ahhh, is it because it would put .00 at the end of it? <BR><BR>Did you leave the word *not* out of that?<BR><BR>&#062; Ahhh, is it because it would *not* put .00 at the end of it? <BR><BR>If that&#039;s what you intended to write, then yes, that&#039;s why your code isn&#039;t "complete".<BR><BR>But don&#039;t ignore the case of 3.5!!! You would have to add a zero to that, as well.<BR><BR>I&#039;m not sure that, in the end, you would get anything a lot shorter than what I wrote. Well, at least you don&#039;t have to handle the sign separately.<BR><BR>&#060;%<BR>Funtion TruncateTwo( ByVal num )<BR>&nbsp; &nbsp; num = CStr(num)<BR>&nbsp; &nbsp; pos = InStr( num, "." )<BR>&nbsp; &nbsp; If pos = 0 Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = num & ".00"<BR>&nbsp; &nbsp; ElseIf pos = Len(num) - 1 Then<BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = num & "0"<BR>&nbsp; &nbsp; Else<BR>&nbsp; &nbsp; &nbsp; &nbsp; TruncateTwo = Left( num, pos+2 )<BR>&nbsp; &nbsp; End If<BR>End Function<BR>%&#062;<BR><BR>Can you do it shorter?<BR><BR>

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•