i can't round

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I can't round my numbers. i have a single, and i'm trying to round it so that
there's only one decimal place. here's what i've tried:

..Val = CStr(Format(sngTimeToTarget, "#0.0"))
and i also tried
..Val = CStr(round(CDbl(sngTimeToTarget), 1))

neither of which do anything. i've even tried multiplying by 10, coercing to
an integer, coercing back to a single, and then dividing by 10 - this will
cause an overflow arithmatic error.

what am i doing wrong?
 
Paul said:
I can't round my numbers. i have a single, and i'm trying to round it so
that
there's only one decimal place. here's what i've tried:

.Val = CStr(Format(sngTimeToTarget, "#0.0"))
and i also tried
.Val = CStr(round(CDbl(sngTimeToTarget), 1))

neither of which do anything. i've even tried multiplying by 10, coercing
to
an integer, coercing back to a single, and then dividing by 10 - this will
cause an overflow arithmatic error.

what am i doing wrong?

fwiw, that looks an awful lot like VB6 code (if it is, this is the wrong
group).... the code below will show a random number between 0 and 100 with 2
decimal places. Note that Round uses bankers rounding (which explains why my
checks bounce <g>) Format uses the rounding I learned in school... that is,
any decimal greater than 5 is rounded up, otherwise, rounded down.

'===
Private Sub Command1_Click()
Debug.Print Format$(Rnd * 100, "0.00")
End Sub
'===

Also note that, in VB, or any other language, you should avoid using
Keywords for property names. Val is definitely a Keyword.
 
Paul said:
I can't round my numbers. i have a single, and i'm trying to round it so
that
there's only one decimal place. here's what i've tried:

.Val = CStr(Format(sngTimeToTarget, "#0.0"))
and i also tried
.Val = CStr(round(CDbl(sngTimeToTarget), 1))

How about:

..Val=Math.Round(CDbl(sngTimeToTarget),1).ToString("#0.0")

Watch out though, the Math.Round by default assumes you want accounting
rounding which is different from the rounding we learned in grade school. In
accounting rounding, 1.5 is rounded to 2 whereas .5 is rounded to 0. The
behavior of this method follows IEEE Standard 754, section 4. This kind of
rounding is sometimes called rounding to nearest, or banker's rounding. If
you want the traditional rounding, you must add an additional parameter to
the Round method as follows:

..Val =
Math.Round(CDbl(sngTimeToTarget),1,MidpointRounding.AwayFromZero).ToString("#0.0")

Jim Wooley
 
Paul said:
I can't round my numbers. i have a single, and i'm trying to round it
so that there's only one decimal place.

\\\
.Val = Math.Round(sngTimeToTarget, 1)
///

Math.Round returns a Double, so if your .Val property is expecting a Single
you'll need to convert it:

\\\
.Val = CSng(Math.Round(sngTimeToTarget, 1))
///

HTH,
 
using round and format in either of those ways doesn't work. i'm sure i've
got my conversions right. the weird thing is that they do work in two other
instances of this custom control, but in the third they don't.

the program runs in vb.net (2003), but it was converted from vb6 by someone
other then myself. i'm learning both in order to take his place.
 
Paul said:
using round and format in either of those ways doesn't work. i'm sure
i've got my conversions right. the weird thing is that they do work
in two other instances of this custom control, but in the third they
don't.

I can only think to say that Round and Format are working (they definitely
do work), it has to be something else in your code that's not doing what you
want.

If you try displaying the results in a MessageBox, does it display the right
value there?

Have you tried stepping into the .Val property assignment to see what's
happening internally?
 

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

Similar Threads

Excel Stop Excel from displaying rounded values 4
Problem With Round Function 9
rounding error 2
Prevent Auto Rounding 2
String to Rounded Integer 3
Rounding issue 4
Excel Excel 2007 Rounding 1
Rounding Problem 5

Back
Top