Rounding-off question from VB newbie.
Author |
Message |
ClockTec #1 / 25
|
 Rounding-off question from VB newbie.
Hi, I'm just getting started in VB, and will probably have a lot of questions. Here's the first: How do I take a number like 1.048175 and round it to 1.05? I tried declaring the variable as currency and it didn't work. I think it's because the math happens after the variable is entered. Here's what I'm doing. txtbox_5.text = a * 1.05. I could do it with a lot of steps, but there must be an easy way to format the output. I'm hoping that one of the gurus here can put me right. Thanks, Tom Chaudoir
|
Tue, 28 Dec 2004 13:36:26 GMT |
|
 |
Neil Moor #2 / 25
|
 Rounding-off question from VB newbie.
It's quite simple, use the following function: Round(expression, number of decimal places to round to). The second parameter defaults to 0, or an integer if you like. So for your purposes this is: txtbox_5.text = a * Round(your number, 2)
Quote: > Hi, > I'm just getting started in VB, and will probably have a lot of questions. > Here's the first: > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > the variable as currency and it didn't work. I think it's because the math > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > = a * 1.05. > I could do it with a lot of steps, but there must be an easy way to format > the output. I'm hoping that one of the gurus here can put me right. > Thanks, > Tom Chaudoir
|
Tue, 28 Dec 2004 15:35:52 GMT |
|
 |
Yaniv Sapi #3 / 25
|
 Rounding-off question from VB newbie.
Quote:
> Hi, > I'm just getting started in VB, and will probably have a lot of questions. > Here's the first: > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > the variable as currency and it didn't work. I think it's because the math > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > = a * 1.05. > I could do it with a lot of steps, but there must be an easy way to format > the output. I'm hoping that one of the gurus here can put me right. > Thanks, > Tom Chaudoir
Look for the Format() function. Yaniv.
|
Tue, 28 Dec 2004 15:23:30 GMT |
|
 |
Jef #4 / 25
|
 Rounding-off question from VB newbie.
x = Round(Replace$(Me.Text1.Text, ".", ","), 2) or x = Round((Me.Text1.Text), 2) Cheers.
Quote: > Hi, > I'm just getting started in VB, and will probably have a lot of questions. > Here's the first: > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > the variable as currency and it didn't work. I think it's because the math > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > = a * 1.05. > I could do it with a lot of steps, but there must be an easy way to format > the output. I'm hoping that one of the gurus here can put me right. > Thanks, > Tom Chaudoir
|
Tue, 28 Dec 2004 20:22:32 GMT |
|
 |
John Guarnier #5 / 25
|
 Rounding-off question from VB newbie.
Here you go. HTH John :) Function Round(X As Double, DP As Integer) As Double On Error GoTo PROC_ERR 'Push the name of the current procedure onto the procedure stack: gErr.Push "Round" 'Your code goes here 'Round (3.56376, 1) will give the result 3.6 'Round (3.56376, 0) will give the result 4 'Round (3.56376, 2) will give the result 3.56 'Round (1.4999, 3) will give the result 1.5 'Round (1.4899, 2) will give the result 1.49 ' '***************************************************************** 'Geoff Stirling: Montreal , PQ, Canada '***************************************************************** 'How to round a number to the nearest value of 5. '12.4 + 2.5 = 14.9 \5 = 2 * 5 = 10 '12.6 + 2.5 = 15.1\5 = 3 * 5 = 15 '18.1 + 2.5 = 20.6\5 = 4 * 5 =20 'Note the case of 12.5 (12.5 + 2.5 = 15\5 = 3 * 5 = 15). This rounds 'up from the midpoint. If you want to round down at the midpoint, you 'might have to use 2.49 or 2.499 etc depending on the precision of your 'data. ' '---Example call:using the Round Function with 2 decimal places 'RoundedPriceWithSalesTax# = Round(PriceSalesTaxNumber#, 2) ' Round = Int((X * 10 ^ DP) + 0.5) / 10 ^ DP PROC_EXIT: 'If the procedure exits normally,Pop the proc name off of the stack: gErr.Pop Exit Function PROC_ERR: gErr.HandleError Resume PROC_EXIT End Function
Quote:
> > Hi, > > I'm just getting started in VB, and will probably have a lot of questions. > > Here's the first: > > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > > the variable as currency and it didn't work. I think it's because the math > > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > > = a * 1.05. > > I could do it with a lot of steps, but there must be an easy way to format > > the output. I'm hoping that one of the gurus here can put me right. > > Thanks, > > Tom Chaudoir > Look for the Format() function. > Yaniv.
|
Tue, 28 Dec 2004 20:39:53 GMT |
|
 |
J Fren #6 / 25
|
 Rounding-off question from VB newbie.
If you are using Doubles (IEE Floating Points) Q# = 10# ^ DP ' /-- round it first N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs DP is number of Decimal Points Personally I would use the Currency type if that is possible in your App.
Quote: >Hi, >I'm just getting started in VB, and will probably have a lot of questions. >Here's the first: >How do I take a number like 1.048175 and round it to 1.05? I tried declaring >the variable as currency and it didn't work. I think it's because the math >happens after the variable is entered. Here's what I'm doing. txtbox_5.text >= a * 1.05. >I could do it with a lot of steps, but there must be an easy way to format >the output. I'm hoping that one of the gurus here can put me right. >Thanks, >Tom Chaudoir
|
Tue, 28 Dec 2004 21:10:19 GMT |
|
 |
Duane Bozart #7 / 25
|
 Rounding-off question from VB newbie.
Using round() or format() is much simpler although it's certainly nice to know how to get there... I would disagree strongly on the general use of Currency (unless it fits the problem) Quote:
> If you are using Doubles (IEE Floating Points) > Q# = 10# ^ DP ' /-- round it first > N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs > DP is number of Decimal Points > Personally I would use the Currency type if that is possible in your > App.
> >Hi, > >I'm just getting started in VB, and will probably have a lot of questions. > >Here's the first: > >How do I take a number like 1.048175 and round it to 1.05? I tried declaring > >the variable as currency and it didn't work. I think it's because the math > >happens after the variable is entered. Here's what I'm doing. txtbox_5.text > >= a * 1.05. > >I could do it with a lot of steps, but there must be an easy way to format > >the output. I'm hoping that one of the gurus here can put me right. > >Thanks, > >Tom Chaudoir
|
Tue, 28 Dec 2004 21:41:44 GMT |
|
 |
Rick Rothstei #8 / 25
|
 Rounding-off question from VB newbie.
The problem may not be as simple as using the Round function. The Round function uses something called Banker's Rounding which might not be what you are looking for. The key is... what do you expect this number to be when rounded to two decimal places -- 1.045? If the answer is 1.05, then DON'T use the Round function. Use one of these instead (they work the same for positive numbers, but differently for negative numbers): Sgn(Number) * Int(0.5 + Abs(Number) * _ 10 ^ Precision) / 10 ^ Precision which always rounds 5's (or greater) away from zero. That is 123.45 and -123.45 become 123.5 and -123.5 respectively. If you always want to round upward (that is -123.45 becomes -123.4 which is larger than 123.45; 123.45 still becomes 123.5), then use this instead Int(0.5 + Number * 10 ^ Precision) / 10 ^ Precision Although the documentation for it is not as prominent as it should be, you can find out about Banker's Rounding in the Remarks section of the Type Conversion Functions entry in VB's help files. (And while it seems to deal only with rounding from one decimal place to an exact integer, it actually deals with any number whose decimal part ends in 5 which is being rounded up one decimal place.) Here is the quote from that section: "When the fractional part is exactly 0.5, CInt and CLng always round it to the nearest even number. For example, 0.5 rounds to 0, and 1.5 rounds to 2. CInt and CLng differ from the Fix and Int functions, which truncate, rather than round, the fractional part of a number. Also, Fix and Int always return a value of the same type as is passed in." Rick
Quote: > Hi, > I'm just getting started in VB, and will probably have a lot of questions. > Here's the first: > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > the variable as currency and it didn't work. I think it's because the math > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > = a * 1.05. > I could do it with a lot of steps, but there must be an easy way to format > the output. I'm hoping that one of the gurus here can put me right. > Thanks, > Tom Chaudoir
|
Tue, 28 Dec 2004 22:39:26 GMT |
|
 |
J Fren #9 / 25
|
 Rounding-off question from VB newbie.
On Fri, 12 Jul 2002 08:41:44 -0500, Duane Bozarth Quote:
>Using round() or format() is much simpler although it's certainly nice >to know how to get there... >I would disagree strongly on the general use of Currency (unless it fits >the problem)
Interesting - why do you dislike Currency ? Historically I avoided it, but for systems where the range is well within -922,337,203,685,477.5808 to 922,337,203,685,477.5807 it seems pretty robust and reliable - after all it is only a 64 bit integer with an implied decimal place. Quote:
>> If you are using Doubles (IEE Floating Points) >> Q# = 10# ^ DP ' /-- round it first >> N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs >> DP is number of Decimal Points >> Personally I would use the Currency type if that is possible in your >> App.
>> >Hi, >> >I'm just getting started in VB, and will probably have a lot of questions. >> >Here's the first: >> >How do I take a number like 1.048175 and round it to 1.05? I tried declaring >> >the variable as currency and it didn't work. I think it's because the math >> >happens after the variable is entered. Here's what I'm doing. txtbox_5.text >> >= a * 1.05. >> >I could do it with a lot of steps, but there must be an easy way to format >> >the output. I'm hoping that one of the gurus here can put me right. >> >Thanks, >> >Tom Chaudoir
|
Tue, 28 Dec 2004 23:08:17 GMT |
|
 |
Duane Bozart #10 / 25
|
 Rounding-off question from VB newbie.
Primarily performance and such "features" as:
10000 0.0001
100000 0
result obtained from them and it is <not> very close to the right answer and there is no telling that. I've done embedded systems on microprocessors where there were no floating point instructions and other such niceties and you can, admittedly, deal with these issues. But, for general programming, I think it is entirely too risky or requires such effort to ensure that such expressions as the above don't creep into the code as to make it a poor choice. (Even in a money problem a division could creep in, I'd warrant, and it may not <always> be the right answer for it to underflow silently.) That said, there are places where it may make sense, but even there there a lot of pitfalls which are very easy to fall into unawares.... hth... Quote:
> On Fri, 12 Jul 2002 08:41:44 -0500, Duane Bozarth
> >Using round() or format() is much simpler although it's certainly nice > >to know how to get there... > >I would disagree strongly on the general use of Currency (unless it fits > >the problem) > Interesting - why do you dislike Currency ? > Historically I avoided it, but for systems where the range is well > within > -922,337,203,685,477.5808 to 922,337,203,685,477.5807 > it seems pretty robust and reliable - after all it is only a 64 bit > integer with an implied decimal place.
> >> If you are using Doubles (IEE Floating Points) > >> Q# = 10# ^ DP ' /-- round it first > >> N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs > >> DP is number of Decimal Points > >> Personally I would use the Currency type if that is possible in your > >> App.
> >> >Hi, > >> >I'm just getting started in VB, and will probably have a lot of questions. > >> >Here's the first: > >> >How do I take a number like 1.048175 and round it to 1.05? I tried declaring > >> >the variable as currency and it didn't work. I think it's because the math > >> >happens after the variable is entered. Here's what I'm doing. txtbox_5.text > >> >= a * 1.05. > >> >I could do it with a lot of steps, but there must be an easy way to format > >> >the output. I'm hoping that one of the gurus here can put me right. > >> >Thanks, > >> >Tom Chaudoir
|
Wed, 29 Dec 2004 01:55:25 GMT |
|
 |
Georg #11 / 25
|
 Rounding-off question from VB newbie.
I'm sure there are more modern routines now but for years I used..............
End Sub Sub RoundSingle(B!) a& = (B! + 0.00005) * 100 B! = a& / 100 End Sub Glenn
Quote: > Primarily performance and such "features" as:
> 10000 0.0001
> 100000 0
> result obtained from them and it is <not> very close to the right answer > and there is no telling that. > I've done embedded systems on microprocessors where there were no > floating point instructions and other such niceties and you can, > admittedly, deal with these issues. But, for general programming, I > think it is entirely too risky or requires such effort to ensure that > such expressions as the above don't creep into the code as to make it a > poor choice. (Even in a money problem a division could creep in, I'd > warrant, and it may not <always> be the right answer for it to underflow > silently.) > That said, there are places where it may make sense, but even there > there a lot of pitfalls which are very easy to fall into unawares.... > hth...
> > On Fri, 12 Jul 2002 08:41:44 -0500, Duane Bozarth
> > >Using round() or format() is much simpler although it's certainly nice > > >to know how to get there... > > >I would disagree strongly on the general use of Currency (unless it fits > > >the problem) > > Interesting - why do you dislike Currency ? > > Historically I avoided it, but for systems where the range is well > > within > > -922,337,203,685,477.5808 to 922,337,203,685,477.5807 > > it seems pretty robust and reliable - after all it is only a 64 bit > > integer with an implied decimal place.
> > >> If you are using Doubles (IEE Floating Points) > > >> Q# = 10# ^ DP ' /-- round it first > > >> N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs > > >> DP is number of Decimal Points > > >> Personally I would use the Currency type if that is possible in your > > >> App.
> > >> >Hi, > > >> >I'm just getting started in VB, and will probably have a lot of questions. > > >> >Here's the first: > > >> >How do I take a number like 1.048175 and round it to 1.05? I tried declaring > > >> >the variable as currency and it didn't work. I think it's because the math > > >> >happens after the variable is entered. Here's what I'm doing. txtbox_5.text > > >> >= a * 1.05. > > >> >I could do it with a lot of steps, but there must be an easy way to format > > >> >the output. I'm hoping that one of the gurus here can put me right. > > >> >Thanks, > > >> >Tom Chaudoir
|
Wed, 29 Dec 2004 02:38:37 GMT |
|
 |
Duane Bozart #12 / 25
|
 Rounding-off question from VB newbie.
I'm not concerned about the rounding question here--I was responding to J French's question as to why I recommended against the general use of Currency data type. The answer still holds--it is potentially dangerous to undetected underflow errors and it's performance is slow relative to floating point (since we're talking about VB, there <is> a fpp). Granted, even floating point will eventually underflow and that can be disastrous, too, but it is much less likely. Of course, I also have to admit that my thinking is biased as I am an engineer/physicist first, programmer second, so my problem domain tends to be one where fp is the "proper" type. For a general ledger, it is <generally> ok and I wouldn't criticize it excessively, but I think I could still find a plausible case in which it could lead to an undesirable result. OP didn't say or imply he was dealing with currency, however, and the recommendation to use Currency was not qualified as to ensuring it was suitable for the application so I just wanted to caution OP not to do that blindly... Quote:
> I'm sure there are more modern routines now but for years I > used..............
> End Sub > Sub RoundSingle(B!) > a& = (B! + 0.00005) * 100 > B! = a& / 100 > End Sub > Glenn
> > Primarily performance and such "features" as:
> > 10000 0.0001
> > 100000 0
> > result obtained from them and it is <not> very close to the right answer > > and there is no telling that. > > I've done embedded systems on microprocessors where there were no > > floating point instructions and other such niceties and you can, > > admittedly, deal with these issues. But, for general programming, I > > think it is entirely too risky or requires such effort to ensure that > > such expressions as the above don't creep into the code as to make it a > > poor choice. (Even in a money problem a division could creep in, I'd > > warrant, and it may not <always> be the right answer for it to underflow > > silently.) > > That said, there are places where it may make sense, but even there > > there a lot of pitfalls which are very easy to fall into unawares.... > > hth...
> > > On Fri, 12 Jul 2002 08:41:44 -0500, Duane Bozarth
> > > >Using round() or format() is much simpler although it's certainly nice > > > >to know how to get there... > > > >I would disagree strongly on the general use of Currency (unless it > fits > > > >the problem) > > > Interesting - why do you dislike Currency ? > > > Historically I avoided it, but for systems where the range is well > > > within > > > -922,337,203,685,477.5808 to 922,337,203,685,477.5807 > > > it seems pretty robust and reliable - after all it is only a 64 bit > > > integer with an implied decimal place.
> > > >> If you are using Doubles (IEE Floating Points) > > > >> Q# = 10# ^ DP ' /-- round it first > > > >> N# = Int(Abs(N#) * Q# + 0.500001) / Q# * Sgn(N#) ' Must do Abs > > > >> DP is number of Decimal Points > > > >> Personally I would use the Currency type if that is possible in your > > > >> App.
> > > >> >Hi, > > > >> >I'm just getting started in VB, and will probably have a lot of > questions. > > > >> >Here's the first: > > > >> >How do I take a number like 1.048175 and round it to 1.05? I tried > declaring > > > >> >the variable as currency and it didn't work. I think it's because > the math > > > >> >happens after the variable is entered. Here's what I'm doing. > txtbox_5.text > > > >> >= a * 1.05. > > > >> >I could do it with a lot of steps, but there must be an easy way to > format > > > >> >the output. I'm hoping that one of the gurus here can put me right. > > > >> >Thanks, > > > >> >Tom Chaudoir
|
Wed, 29 Dec 2004 04:16:21 GMT |
|
 |
Rick Rothstei #13 / 25
|
 Rounding-off question from VB newbie.
Quote: > OP didn't say or imply he was dealing with currency
Actually, he pretty much said he wasn't since the example data in his question was a number with 6 decimal places (1.048175) and, of course, the Currency data type only hold 4 decimal places. True, he asked for rounding to 2 decimal places but we don't know anywhere enough about what he's doing to recommend using the Currency data type. He could just as easily be interested in rounding to 5 decimal places later on. Rick
|
Wed, 29 Dec 2004 05:06:21 GMT |
|
 |
#14 / 25
|
 Rounding-off question from VB newbie.
|
Wed, 18 Jun 1902 08:00:00 GMT |
|
 |
ClockTec #15 / 25
|
 Rounding-off question from VB newbie.
Many thanks to all who replied. The Round function worked out really well. For the record, I am dealing in currency. The program is a little calculator for Wisconsin sales taxes. Best, Tom Chaudoir Milwaukee, WI America
Quote: > Hi, > I'm just getting started in VB, and will probably have a lot of questions. > Here's the first: > How do I take a number like 1.048175 and round it to 1.05? I tried declaring > the variable as currency and it didn't work. I think it's because the math > happens after the variable is entered. Here's what I'm doing. txtbox_5.text > = a * 1.05. > I could do it with a lot of steps, but there must be an easy way to format > the output. I'm hoping that one of the gurus here can put me right. > Thanks, > Tom Chaudoir
|
Wed, 29 Dec 2004 14:19:20 GMT |
|
|
Page 1 of 2
|
[ 25 post ] |
|
Go to page:
[1]
[2] |
|