?
Solved

How do I Trim a decimal from 57.3242456436543 to 57.324?

Posted on 2005-03-02
14
Medium Priority
?
191 Views
Last Modified: 2010-04-23
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!!!
0
Comment
Question by:indy500fan
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
  • 3
  • +2
14 Comments
 
LVL 21

Expert Comment

by:oleggold
ID: 13442468
Hi indy500fan,
You can do it with double datatype conversion,I'll work on it
Hope It helps
Cheers!
0
 
LVL 21

Expert Comment

by:oleggold
ID: 13442543
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
 
LVL 21

Assisted Solution

by:oleggold
oleggold earned 900 total points
ID: 13442570
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 21

Expert Comment

by:oleggold
ID: 13442583
What's the problem,pls explain it more detailed,Thanks
0
 
LVL 21

Expert Comment

by:oleggold
ID: 13442612
When You are displaying it's has been rounded (in my listbox for sure) for any number of displays
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13442642
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
 
LVL 34

Accepted Solution

by:
Brian Crowe earned 900 total points
ID: 13442673
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
 
LVL 86

Assisted Solution

by:Mike Tomlinson
Mike Tomlinson earned 200 total points
ID: 13442733
>> 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
 
LVL 86

Expert Comment

by:Mike Tomlinson
ID: 13442758
Sorry BriCrowe....didn't refresh.
0
 

Author Comment

by:indy500fan
ID: 13443624
Wow, that's a lot of input.

Thanks to all of you.  How do I split the points on this one???
0
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13443644
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
 

Author Comment

by:indy500fan
ID: 13443725
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
 
LVL 12

Expert Comment

by:S-Twilley
ID: 13443745
sure... since you put it like that :P
0
 

Author Comment

by:indy500fan
ID: 13443760
too funny!
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question