Solved

Remove extra zeros

Posted on 2011-09-05
14
580 Views
Last Modified: 2012-05-12
Hi

How do I remove "extra" zeros from end of string
e.g. replace 2.3100 with 2.31 and 4.4400000 with 4.44
(I don't simply want to round the number, rather remove extra 0's)

Thanks
0
Comment
Question by:rwallacej
[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
  • 4
  • 3
  • 2
  • +5
14 Comments
 
LVL 9

Expert Comment

by:jerrypd
ID: 36484343
i would start at the end of the string lopping off a character at a time until i got to a non-zero character.
There are probably better ways, but this would work.
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 36484348
0
 
LVL 25

Expert Comment

by:Luis Pérez
ID: 36484379
Dim s As String = "4.4400000"
s = s.TrimEnd("0") 'Now trailing zeros are removed, string contains "4.44"

Hope that helps.
0
Independent Software Vendors: 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!

 

Author Comment

by:rwallacej
ID: 36484394
thanks,

this is ok sometimes but when the value is e.g.    40 it is changed to "4", or 1000 it is changed to "1"

I don't want the number to change just remove extra decimal "0"
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 36484400
0
 

Author Comment

by:rwallacej
ID: 36484462
example please?  I can't see how the trim will remove extra zeros only after decimal points
0
 
LVL 38

Assisted Solution

by:PaulHews
PaulHews earned 250 total points
ID: 36484501
This should work to remove extra zeroes:
Public Class Form1

    Private Sub Form1_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        MsgBox(FormatString("23.45600000"))
    End Sub
    Private Function FormatString(ByVal StringVal As String) As String
        Dim dbl As Double
        Double.TryParse(StringVal, dbl)
        Return dbl.ToString("g")
    End Function
End Class

Open in new window

0
 
LVL 15

Expert Comment

by:x77
ID: 36484502
try this function - NormalizeDouble.
Function NormalizeDouble(ByVal s As String) As String
    Dim v As Double
    If Double.TryParse(s, v) Then s = v.ToString("g")
    Return s
End Function

Open in new window

0
 
LVL 16

Expert Comment

by:disrupt
ID: 36485094
0
 
LVL 19

Accepted Solution

by:
Shahan Ayyub earned 250 total points
ID: 36485147
As an alternative, you can use:

       Dim temp as string = Regex.Replace("2.3100000", "(\.?[0]+$)", "")

Open in new window


You need to add at top of your code file:

Imports System.Text.RegularExpression
0
 

Author Closing Comment

by:rwallacej
ID: 36488837
thanks all
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36488866
Did you try the solution I posted?
0
 

Author Comment

by:rwallacej
ID: 36489336
no,  I hadn't tried this, a long article and other solution worked - this may have worked, too
0
 
LVL 16

Expert Comment

by:disrupt
ID: 36489363
It was not long! It was only:

string.Format("{0:G29}", decimal.Parse("2.0044"))
or
decimal.Parse("2.0044").ToString("G29")
or
2.0m.ToString("G29")
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
This video Micro Tutorial shows how to password-protect PDF files with free software. Many software products can do this, such as Adobe Acrobat (but not Adobe Reader), Nuance PaperPort, and Nuance Power PDF, but they are not free products. This vide…
If you're a developer or IT admin, you’re probably tasked with managing multiple websites, servers, applications, and levels of security on a daily basis. While this can be extremely time consuming, it can also be frustrating when systems aren't wor…

691 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