Solved

Remove extra zeros

Posted on 2011-09-05
14
574 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
  • 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

813 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now