rwallacej
asked on
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
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
Dim s As String = "4.4400000"
s = s.TrimEnd("0") 'Now trailing zeros are removed, string contains "4.44"
Hope that helps.
s = s.TrimEnd("0") 'Now trailing zeros are removed, string contains "4.44"
Hope that helps.
ASKER
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"
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"
ASKER
example please? I can't see how the trim will remove extra zeros only after decimal points
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
thanks all
Did you try the solution I posted?
ASKER
no, I hadn't tried this, a long article and other solution worked - this may have worked, too
It was not long! It was only:
string.Format("{0:G29}", decimal.Parse("2.0044"))
or
decimal.Parse("2.0044").To String("G2 9")
or
2.0m.ToString("G29")
string.Format("{0:G29}", decimal.Parse("2.0044"))
or
decimal.Parse("2.0044").To
or
2.0m.ToString("G29")
There are probably better ways, but this would work.