• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 590
  • Last Modified:

Remove extra zeros

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
rwallacej
Asked:
rwallacej
  • 4
  • 3
  • 2
  • +5
2 Solutions
 
jerrypdCommented:
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
 
plusone3055Commented:
0
 
Luis PérezSoftware Architect in .NetCommented:
Dim s As String = "4.4400000"
s = s.TrimEnd("0") 'Now trailing zeros are removed, string contains "4.44"

Hope that helps.
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
rwallacejAuthor Commented:
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
 
plusone3055Commented:
0
 
rwallacejAuthor Commented:
example please?  I can't see how the trim will remove extra zeros only after decimal points
0
 
PaulHewsCommented:
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
 
x77Commented:
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
 
Shahan AyyubSenior Software Engineer - iOSCommented:
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
 
rwallacejAuthor Commented:
thanks all
0
 
disruptCommented:
Did you try the solution I posted?
0
 
rwallacejAuthor Commented:
no,  I hadn't tried this, a long article and other solution worked - this may have worked, too
0
 
disruptCommented:
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

  • 4
  • 3
  • 2
  • +5
Tackle projects and never again get stuck behind a technical roadblock.
Join Now