Solved

# How do I Trim a decimal from 57.3242456436543 to 57.324?

Posted on 2005-03-02
Medium Priority
191 Views
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.

0
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
• 5
• 3
• 3
• +2

LVL 21

Expert Comment

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

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)
End Sub
0

LVL 21

Assisted Solution

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)
dd = Math.Round(ds, 3)
Me.ListBox1.Items.Add(d & " to " & dd)
0

LVL 21

Expert Comment

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

LVL 21

Expert Comment

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

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

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

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

ID: 13442758
Sorry BriCrowe....didn't refresh.
0

Author Comment

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

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

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

ID: 13443745
sure... since you put it like that :P
0

Author Comment

ID: 13443760
too funny!
0

## Featured Post

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