
Truncating decimals
I'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't want to round tho. Please help. <BR><BR>Thanks!<BR>LindaBEE

RE: Truncating decimals
FormatNumber(yourvalue,2)<BR>BTW, check out the VBScript reference at the left...lots more builtin functions there.

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)

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

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

Hungarian got you again!
If the variable is truly called "intNumber" then it is *supposed* to be holding *only* integers.<BR><BR>SO let's say:<BR><BR>intNumber = 7<BR>pos = InStr( intNumber, "." ) <BR> ' Hmmm...so since there is no decimal point, pos is zero<BR>pos = pos + 2 ' so now pos is 2<BR>left(intNumber,pos) ' 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>

Grrr
You'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't bitten me yet, but I'm sure it will in the future. I submit humbly to the intense bashing I'm about to receive...

You're right...
It's a heluva lot simpler than mine.<BR><BR>At first, I thought it wouldn'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't really do the job.<BR><BR>

Not sure I understand
Why wouldn't this work with money? Ahhh, is it because it would put .00 at the end of it? *Technically* that's the correct 2 digits after the decimal point, isn't it?<BR>

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>> 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>> Ahhh, is it because it would *not* put .00 at the end of it? <BR><BR>If that's what you intended to write, then yes, that's why your code isn't "complete".<BR><BR>But don't ignore the case of 3.5!!! You would have to add a zero to that, as well.<BR><BR>I'm not sure that, in the end, you would get anything a lot shorter than what I wrote. Well, at least you don't have to handle the sign separately.<BR><BR><%<BR>Funtion TruncateTwo( ByVal num )<BR> num = CStr(num)<BR> pos = InStr( num, "." )<BR> If pos = 0 Then<BR> TruncateTwo = num & ".00"<BR> ElseIf pos = Len(num)  1 Then<BR> TruncateTwo = num & "0"<BR> Else<BR> TruncateTwo = Left( num, pos+2 )<BR> End If<BR>End Function<BR>%><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

Forum Rules

