473,327 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

i can't round

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?
Jan 25 '06 #1
6 1491
"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:0D**********************************@microsof t.com...
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.

--
Ken Halter - MS-MVP-VB (visiting from VB6 world) - http://www.vbsight.com
Please keep all discussions in the groups..
Jan 25 '06 #2
"Paul" <Pa**@discussions.microsoft.com> wrote in message
news:0D**********************************@microsof t.com...
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,MidpointRoundin g.AwayFromZero).ToString("#0.0")

Jim Wooley
Jan 25 '06 #3
Paul wrote:
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,

--

(O)enone
Jan 25 '06 #4
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.
Jan 25 '06 #5
wait, when i do them both at the same time, it does work.
Jan 25 '06 #6
Paul wrote:
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?

--

(O)enone
Jan 25 '06 #7

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Guido Braceletti | last post by:
I'm ok with : round(118.5) => 119 round(118.15, 1) => 118.2 But why : round(118.815, 2) => 118.81 ??? why not 118.82 ? Thanks for Help
14
by: Nils Grimsmo | last post by:
Why did round() change in Python 2.4? $ python2.3 Python 2.3.5 (#2, Jun 19 2005, 13:28:00) on linux2 >>> round(0.0225, 3) 0.023 >>> "%.3f" % round(0.0225, 3) '0.023' >>>
10
by: tmeister | last post by:
I'm trying to determine the best approach for rounding in an application I'm building. Unfortunately it appears as though SQL Server and VB.NET round in different ways. SQL Server select...
9
by: Ronald W. Roberts | last post by:
I'm having a problem understanding the Round function. Below are quotes from two books on VB.NET. The first book shows examples with one argument and how it rounds. The second book something...
5
by: karmalax | last post by:
public static string Somme(string T) { string text1 = ""; double num5 = Strings.Len(T) + 1; int num3 = 7;
10
by: David Coleman | last post by:
I am running VS 2003 and have applied SP1. (On WinXP SP2, .Net 1.1) In the Command Window I get the following ? Math.Round(0.715, 2) 0.72 ? Math.Round(0.725, 2) 0.72 ? Math.Round(0.735, 2)...
7
by: kkmigas | last post by:
Can some one explain if this can be fixed using php.ini settings ? echo "round 20.545 -".round(20.545,2)."<br>"; echo "round 20.555 -".round(20.555,2)."<br>"; echo "number_format 20.545...
4
by: Jassim Rahma | last post by:
I have a number, for example 0.152 or 1.729 and I want to allow to round to the first 0.010 or 0.050 or 0.100
4
by: =?Utf-8?B?UmVuZQ==?= | last post by:
Hello everyone I have a problem with Math.Round, it´s ocurring some strange: Math.Round(12.985) = 12.98, it´s wrong. It should be: 12.99 Why?? What is the problem? Help ME !!!!
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.