How do I Trim a decimal from 57.3242456436543 to 57.324?

How do I trim a decimal to only 3 significant places?

I do the calculation:

Speed = Distance / ElapsedTime

and I might get 57.3242456436543 for example.

Then, as the very next line of code, I have:

Math.Round(Speed, 3)

Now, when I display Speed, has that number been rounded (meaning anytime I reference Speed it is 57.324, until it is recalculated), or should I do it within the calculation?

Unfortunately, I can't test this, and I won't be able to until tomorrow.  That is why I am asking you experts in hopes that you can test it for me.  This is fairly urgent so I'm putting up 500points.

Thanks in advance!!!
indy500fanAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

oleggoldCommented:
Hi indy500fan,
You can do it with double datatype conversion,I'll work on it
Hope It helps
Cheers!
0
oleggoldCommented:
This worked fine for me:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '     Dim s As Date = CDate(Me.TextBox1.Text)
        'i += 1
        'Me.TextBox1.Text = i
        'Dim ds As System.Data.DataSet
        'Me.ListBox1.MultiColumn = True
        'Me.ListBox1.DataSource = ds
        Dim d As Double = 57.3242456436543
        Dim dd As Double
        dd = Math.Round(d, 2)
        Me.ListBox1.Items.Add(d & " to " & dd)
        Me.ListBox1.Items.Add(":" & dd)
    End Sub
0
oleggoldCommented:
the same for 53.324:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        '     Dim s As Date = CDate(Me.TextBox1.Text)
        'i += 1
        'Me.TextBox1.Text = i
        'Dim ds As System.Data.DataSet
        'Me.ListBox1.MultiColumn = True
        'Me.ListBox1.DataSource = ds
        Dim d, ds As Double
        d = 57.3242456436543
        ds = 57.324
        Dim dd As Double
        dd = Math.Round(d, 3)
        Me.ListBox1.Items.Add(d & " to " & dd)
        Me.ListBox1.Items.Add(":" & dd)
        dd = Math.Round(ds, 3)
        Me.ListBox1.Items.Add(d & " to " & dd)
        Me.ListBox1.Items.Add(":" & dd)
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

oleggoldCommented:
What's the problem,pls explain it more detailed,Thanks
0
oleggoldCommented:
When You are displaying it's has been rounded (in my listbox for sure) for any number of displays
0
S-TwilleyCommented:
Just for reading, this question is still open and I've posted an alternative to using the Round method (the user stated their own problems with using that method).

http://www.experts-exchange.com/Programming/Programming_Languages/Dot_Net/VB_DOT_NET/Q_21321033.html
0
Brian CroweDatabase AdministratorCommented:
I would suggest that you never actually round the number, if you need to use the number in future calculations then you will lose accuracy.  Just format it to the number of digits you need on display.

If Speed is a decimal then...

messagebox.show(Speed.tostring("0.000"))  'this will round and display the first three decimal places i.e. 57.324

If Speed is a double then...
messagebox.show(cdec(speed).tostring("0.000"))
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Mike TomlinsonMiddle School Assistant TeacherCommented:
>> Now, when I display Speed, has that number been rounded (meaning anytime I reference Speed it is 57.324, until it is recalculated)

        Dim speed As Double = 57.3242456436543
        Debug.WriteLine(speed) ' 57.3242456436543
        speed = Math.Round(speed, 3)
        Debug.WriteLine(speed) ' 57.324

Yes...after the the call to Math.Round(), the number has been rounded and all the precision past the third decimal place is now gone.  If you still need the precision for calculations then display it using a Format() statement instead:

        Dim speed As Double = 57.3242456436543
        Debug.WriteLine(speed) ' 57.3242456436543
        Debug.WriteLine(Format(speed, "#.000")) ' 57.324

~IM
0
Mike TomlinsonMiddle School Assistant TeacherCommented:
Sorry BriCrowe....didn't refresh.
0
indy500fanAuthor Commented:
Wow, that's a lot of input.

Thanks to all of you.  How do I split the points on this one???
0
S-TwilleyCommented:
Think you should just split it up amongst the people who helped you to come to your conclusion/answer... Im not fussed as I only referred you to a "discussion" so it was kinda cheating :P
0
indy500fanAuthor Commented:
Okay guys, here's my reasoning:

oleggold - I gave you 225 because you gave me a means of troubleshooting with yours.

BriCrowe - I gave you 225 because you gave me a reason as to your answer, and you were really heading down the path I was looking for...

Idle Mind - Even though BriCrowe beat you to the punch, you confirmed what I was suspecting...

And S-Twilley, even though you are one of my favorite experts, I felt like the other guys got there first... Still friends?
0
S-TwilleyCommented:
sure... since you put it like that :P
0
indy500fanAuthor Commented:
too funny!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Visual Basic.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.