Click here to Skip to main content
16,017,608 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
var amt = "2.8-";

double reqamt = Double.Parse(climateString.Replace(",", ""));
            MessageBox.Show(reqamt.ToString());



I am getting below exception:
"Input string was not in a correct format."



What I have tried:

This is working fine when var amt = "-2.8";

Can I get this working when var amt = "2.8-";

Because Database sends me results like "2.8-";
Posted
Updated 19-Oct-16 10:38am
Comments
[no name] 19-Oct-16 15:17pm    
Okay.... so what is your question then if you know what the problem is?
Member 12478311 19-Oct-16 15:19pm    
My question is..

How can I make it work when var amt = "2.8-";
ZurdoDev 19-Oct-16 15:21pm    
Remove the end -.
[no name] 19-Oct-16 15:23pm    
Why ask a question when you already know the answer? Fix the data.
Member 12478311 19-Oct-16 15:36pm    
I am new to programming.
and I want to know if c# can handle those values.
This is my question.

Don't use Parse. Use TryParse instead.

You can do this quiet easily. You just have to use the version of TryPrase that allows you to specify the FormatProvider, which describes how to parse the string into a Double. Read the documentation on Double for more information. It helps to create little test programs to try out something you're not sure of before dropping code into the app you're working on.

For example:
C#
using System;
using System.Globalization;

namespace TryParse_Sandbox
{
    class Program
    {
        static void Main(string[] args)
        {
            string[] input = { "2.8", "-2.8", "2.8-", "0", "-2", "2-" };

            NumberStyles style = NumberStyles.AllowDecimalPoint |
                NumberStyles.AllowLeadingSign |
                NumberStyles.AllowTrailingSign |
                NumberStyles.AllowThousands;

            CultureInfo culture = CultureInfo.CurrentUICulture;

            foreach (var number in input)
            {
                double value;

                if (double.TryParse(number, style, culture, out value))
                    Console.WriteLine($"String: '{number}'\tParsed: {value}");
                else
                    Console.WriteLine($"Failed to parse string: '{number}'");
            }
        }
    }
}
 
Share this answer
 
Comments
CPallini 20-Oct-16 5:51am    
Well, Knowledge brings a superior solution, have my 5.
You have to parse yourself the trailing sign. It is not difficult: if the string features a trailing minus ('-') then use double.Parse on the leading substring to get the absolute value and eventually negate it.

Something like:
C#
static double ParseDBNumber(string  s)
{
  double result;
  if ( s.Substring(s.Length-1) == "-")
  {
    result = double.Parse(s.Substring(0, s.Length - 1));
    return  (-result);
   }
   return double.Parse(s);
 }
 
Share this answer
 
Comments
ZurdoDev 19-Oct-16 16:23pm    
if (s.EndsWith("-")) // easier

+5 though.
CPallini 20-Oct-16 5:50am    
Right! Thank you.
Dave Kreskowiak 19-Oct-16 16:40pm    
You don't have to do this at all. TryPrase allows you to specify how to parse the number, including trailing signs.
CPallini 20-Oct-16 5:52am    
I acknowledge your superior solution.
Karthik_Mahalingam 19-Oct-16 23:48pm    
5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900