Solved

Remove extra zeros

Posted on 2011-09-05
14
569 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
 

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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Limiting string to two deciamls 18 34
Setting runtime form location 4 19
Showdialog 8 21
Vb.Net Date Formatting Assistance 4 16
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

759 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

18 Experts available now in Live!

Get 1:1 Help Now