|
-
Aug 30th, 2000, 09:44 AM
#1
Thread Starter
Hyperactive Member
How can I make sure a number is always rounded up, regardless of what follows the decimal point
Int() and Fix() seem to caouse it to be rounded down.
-
Aug 30th, 2000, 09:48 AM
#2
Frenzied Member
-
Aug 30th, 2000, 09:52 AM
#3
Hyperactive Member
Well I guess depending on how many decimal points you can have you could always add 0.999.... to the number and then round it, but don't give me a hard time on this one if it is complete rubbish, I am on my lunch break here.
-
Aug 30th, 2000, 10:01 AM
#4
Fanatic Member
Results using the ROUND() function recommended by kovan.
? ROUND(1234.1962, 3) && Displays 1234.1960
? ROUND(1234.1962, 2) && Displays 1234.2000
? ROUND(1234.1962, 0) && Displays 1234.0000
? ROUND(1234.1962, -1) && Displays 1230.0000
? ROUND(1234.1962, -2) && Displays 1200.0000
? ROUND(1234.1962, -3) && Displays 1000.0000
Syntax
ROUND(expression[,numdecimalplaces])
If you omit the numdecimalplaces, an integer is returned.
-
Aug 30th, 2000, 10:03 AM
#5
Thread Starter
Hyperactive Member
well, i've kinda sorted it like this, but it's a dodgy as hell kinda way of doing it: -
Code:
Public Function RoundUp(Num As Long) As Integer
Dim NumStr, TempStr1, TempStr2 As String
Dim X As Integer
NumStr = CStr(Num)
TempStr1 = ""
TempStr2 = ""
For X = 1 To Len(NumStr)
TempStr1 = Mid(NumStr, X, 1)
If TempStr1 = "." Then
Exit For
Else
TempStr2 = TempStr2 + TempStr1
End If
Next X
RoundUp = (CInt(TempStr2) + 1)
End Function
there has to be a mathematical way of doing this - but i struggle to add up, never mind do hard stuff
-
Aug 30th, 2000, 10:06 AM
#6
Hyperactive Member
For info. VB does some strange rounding when it rounds a decimal to an integer on your behalf. When the decimal is in the middle ( .5 ) then for odd numbers it rounds up and evens number round down ! 1.5 and 2.5 are both rounded to 2.
Try this code if you don't beleive me :
Code:
Private Sub Form_Load()
Dim sTemp1 As String
Dim sTemp2 As String
Dim iTemp1 As Integer
Dim iTemp2 As Integer
' Remember this lesson when changing values from string to integer
sTemp1 = "1.5"
sTemp2 = "2.5"
iTemp1 = Val(sTemp1)
iTemp2 = Val(sTemp2)
MsgBox iTemp1 & " " & iTemp2, vbOKOnly, "VB Rounding"
End Sub
That's Mr Mullet to you, you mulletless wonder.
-
Aug 30th, 2000, 10:16 AM
#7
Fanatic Member
Well, then ROUND() is not going to be very reliable.
In the immediate window you get.
? ROUND(1.5, 0)
2
? ROUND(2.5, 0)
2
Paul, thanks for passing that on.
-
Aug 30th, 2000, 10:44 AM
#8
_______
<?>
Code:
Option Explicit
'rounding a number
Function RoundNumber(lNumber, Optional iDecimalPlaces As Integer = 1)
RoundNumber = Int(lNumber * (10 ^ iDecimalPlaces) + 0.5) / (10 ^ iDecimalPlaces)
End Function
'3 represents the number of places you want after the decimal
Private Sub Command1_Click()
MsgBox RoundNumber(34567.8987, 3)
End Sub
"A myth is not the succession of individual images,
but an integerated meaningful entity,
reflecting a distinct aspect of the real world."
___ Adolf Jensen
-
Aug 30th, 2000, 11:27 AM
#9
Thread Starter
Hyperactive Member
that last one seems to round up OR down depending upon the value of the leftmost digit being removed, as normal meths would have it, but for my situation I am converting days to months for insurance cover - ie 35 days needs to be rounded up into a 2 month policy...
-
Aug 30th, 2000, 11:47 AM
#10
Fanatic Member
Do you consider one month as 31 days? If yes then try this:
Code:
Dim inDays As Integer: inDays = Text1.Text
Dim inMonths As Integer
If inDays Mod 31 <> 0 Then
inMonths = inDays \ 31 + 1
Else
inMonths = inDays \ 31
End If
MsgBox inMonths '//If inDays = 35 then inMonths returns 2
HTH
-
Aug 31st, 2000, 06:20 AM
#11
Thread Starter
Hyperactive Member
OK, similar thing, now
how can I force a number to have 2 decimal places??
ie 3 would appear as 3.00
tried Round(3,2), but just reduces the number of decimal places to 2, rather than increasing them
-
Aug 31st, 2000, 06:25 AM
#12
Fanatic Member
Look up the Format command and see the different ways it allows you to format numbers, dates, strings.
For example: format(3,"#.00")
returns 3.00
format(3.9,"#.00")
returns 3.90
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
|
Click Here to Expand Forum to Full Width
|