Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7506089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T22:08:33+00:00 2026-05-29T22:08:33+00:00

I have some customized string formats of double values. I need to convert these

  • 0

I have some customized string formats of double values. I need to convert these strings to double values at some point (Note that these strings may contain error messages other than numbers). So I first check if these strings are numbers, and if yes, then convert them to double. Isnumeric works for my first customized string format. But I found that is numeric cannot recognize percentage strings. I wonder why isnumeric understands () but cannot understand %. And what is the better way to convert “(100.00)” and “50%” to doubles other than Cdbl?

Private Format As String = "###,###,###,##0.00;(###,###,###,##0.00);0" 'negative values are in the format (number)
Private rateFormat as string="p"
Dim str1 as string=-100.0.Tostring(Format)  'str1=(100.0)
Dim str2 as string=0.5.Tostring(rateFormat) 'str2=50%
Dim value1,value2 as double
If isnumeric(str1)
value1=Cdbl(str1) 'True. value1=-100.0
else
value1=Double.MinValue
End If
If isnumeric(str2)
value2=cdbl(str2) 'False.value2=Double.Minvalue
else
value2=Double.MinValue
End If
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T22:08:34+00:00Added an answer on May 29, 2026 at 10:08 pm

    First, IsNumeric and C<whatever> generally aren’t used because they can provide unexpected outputs. Typically you would use Double.TryParse or Convert.ToDouble (or whatever type) because they reliably parses numbers as you would expect; you just have to make sure it’s in the right format.

    Also, those methods are more efficient because you can determine if a string is a number and parse it all in one go. Just note that Parse and Convert throw exceptions when the format is wrong.

    Now, as far as I know, there’s know built-in way convert from percent notation. I went ahead and wrote three methods (current culture, any culture, invariant culture) that tested fine with 0.5 and -0.5 on the four possible locations of the percent symbol.

    'these will return Double.NaN if they fails
    'this parses based on the current culture
    Public Function ParseDoublePercent(ByVal input As String) As Double
        Return ParseDoublePercent(input, System.Globalization.NumberFormatInfo.CurrentInfo)
    End Function
    
    'invariant culture
    Public Function ParseDoublePercentInvariant(ByVal input As String) As Double
        Return ParseDoublePercent(input, System.Globalization.NumberFormatInfo.InvariantInfo)
    End Function
    
    'this parses based on a specified culture
    Public Function ParseDoublePercent(ByVal input As String, ByVal provider As IFormatProvider) As Double
        If String.IsNullOrEmpty(input) OrElse input.Length < 3 Then Return Double.NaN 'minimum length of string is 2 (0%)
        'get some information about how the percent should be formatted
        Dim format As System.Globalization.NumberFormatInfo = System.Globalization.NumberFormatInfo.GetInstance(provider)
    
        'how this will be parsed will first depend on if it's positive or negative
        Dim isNegative As Boolean = input.Substring(0, 1) = "-"
        Dim percentPattern As Integer = If(isNegative, format.PercentNegativePattern, format.PercentPositivePattern)
    
        'i'm using convert.todouble because I don't want to use NumberStyles in Double.TryParse
        Try
            Select Case percentPattern
                Case 0 'n %, such as -50.00 %, 50.00 %
                    Return Convert.ToDouble(
                        input.Replace(" " & format.PercentSymbol, "e-" & format.PercentDecimalDigits),
                        provider)
                Case 1 'n%, such as -50.00%, 50.00%
                    Return Convert.ToDouble(
                        input.Replace(format.PercentSymbol, "e-" & format.PercentDecimalDigits),
                        provider)
                Case 2 '%n, such as -%50.00, %50.00
                    'need to do some special parsing just in case there are incorrect percent signs in the middle of the number
                    'dont want to just replace all of them
                    Return Convert.ToDouble(
                            input.Remove(If(isNegative, 1, 0), 1) & "e-" & format.PercentDecimalDigits, provider)
                Case 3 '% n, such as '%-50.00, % 50.00
                    Return Convert.ToDouble(input.Remove(0, 1) & "e-" & format.PercentDecimalDigits, provider)
                Case Else
                    Return Double.NaN 'should not happen
            End Select
        Catch ex As FormatException 'an exception is thrown when the string fails to parse
            Return Double.NaN
        End Try
    End Function
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that if I have some images and subviews added in customized cell
I have a custom winforms control (inherits from Component) that has some strings displayed
We are developing a customized installer using Wix and Wpf. We have developed some
I have some UI in VB 2005 that looks great in XP Style, but
We have some input data that sometimes appears with &nbsp characters on the end.
We have some files on our website that users of our software can download.
I have panel that I have customized. I use it to display text. But
I have a JUnit test that is checking to make sure that a customized
I have a class but I want some customized behavior if a field is
I have some ASP.NET web services which all share a common helper class they

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.