Results 1 to 17 of 17

Thread: [RESOLVED] Adding decimal in textbox and calculation

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Resolved [RESOLVED] Adding decimal in textbox and calculation

    I have 3 textboxes, 2 input boxes and a results box what i would like to do is to be able to add a value in the textboxes including a decimal point.
    Code:
             int txtPTextBox;
                int txtVTextBox;
                int txtPxVTextBox;
                
                txtPTextBox = int.Parse(txtP.Text);
                txtVTextBox = int.Parse(txtV.Text);
                txtPxVTextBox = int.Parse(txtPxV.Text);
    
                if (radioButtonPV.Checked == true)
                {
                   
                    {
                        if (txtPTextBox < 1.5)
    //............
    However i get an error pointing to the int.parse if i write a decimal value.
    Any tips on how to allow a decimal value?

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Adding decimal in textbox and calculation

    Integers, which is what an int is, don't allow decimal values. Try declaring the variables as decimal and using decimal.Parse instead.

  3. #3

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    Stupid me, should have known that.

    However i now get a error on the values.
    Code:
     if (txtPTextBox < 1.5)
    Operator '<' cannot be applied to operands of type 'decimal' and 'double'
    This error only underlines the decimal values. Is it not possible to compare "1.5"?

  4. #4
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Adding decimal in textbox and calculation

    The compiler treats 1.5 as a double rather than a decimal, depending on your needs you could either force the value to be a decimal by using
    Code:
    (if txtPTextBox < 1.5m)
    or declare your variables as double rather than decimal.

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding decimal in textbox and calculation

    Imagine someone who has never seen your code before. What would you expect the name 'txtPTextBox' to indicate to them? I am such a person and it indicates to me that it refers to a TextBox but not what the purpose of that TextBox is. Both of those are bad things. If the variable is of a numeric data type then the name should reflect that and the name should also indicate what that number represents.

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    I understand, txtPTextbox indicates a Pressure whilst txtVTextBox indicates a volume.
    if someone enters a value of < 1.5 bar (1,5 bar) the program needs to display a text if it is above this value it should display an other text. It works fine when i use integers, but not with decimal numbers.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding decimal in textbox and calculation

    Quote Originally Posted by rakker View Post
    I understand, txtPTextbox indicates a Pressure whilst txtVTextBox indicates a volume.
    As a trained chemist and chemical engineer, I thought that that was likely. I thought that there might be some other possibilities too though.

    Given that you know the specific purpose of your app, it may seem obvious to you what they are but they may not be to others or even to you if you leave this project and come back to it later. I would suggest not using Hungarian nothation at all but, if you are going to use it, make sure that you use it properly. "txt" is an appropriate prefix for a TextBox variable but definitely not for an 'int' or 'decimal'. The original prefix for integers was 'n' but most people use 'int' now. 'dec' is what most people use for decimals. Personally, I would use 'pressureTextBox' and 'pressure' as the variable names for the TextBox containing the pressure value and the actual pressure value as they tell you explicitly and unambiguously what they are. They don't rely on prior knowledge or additional comments to be clear and, most importantly, they don't mislead. If you want to stick with Hungarian notation and abbreviation though, use 'txtP' and 'decP'.
    Quote Originally Posted by rakker View Post
    if someone enters a value of < 1.5 bar (1,5 bar) the program needs to display a text if it is above this value it should display an other text. It works fine when i use integers, but not with decimal numbers.
    Just telling us that something doesn't work is rarely enough. You need to tell us what actually happens. If you did it right then it would work so you must have done it wrong. Show us what you did and tell us what happened when you did it and then we can most likely tell you what's wrong and what needs to change. If an exception is thrown, point out exactly where. We should never have to figure stuff out that you already know. If you provide all the relevant info, we'll probably spot the error pretty quickly, as we'll have seen (and done) similar things many times.

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    The following happens.

    On clicking the calculate button i get an error.
    "An unhandled exception of type 'System.FormatException' occurred in mscorlib.dll"

    Code:
      decimal PressureTextBox;
               decimal VolumeTextBox;
                decimal PxVTextBox;
    
                PressureTextBox = decimal.Parse(Pressure.Text); 
                VolumeTextBox = decimal.Parse(Volume.Text);
               PxVTextBox = decimal.Parse(PxV.Text);
    The error points to: PressureTextBox = decimal.Parse(Pressure.Text);
    I understand that it points to this textbox since i tried the input value "12.5" and the system does not understand this value as far as i can see.
    Last edited by rakker; Jun 14th, 2017 at 02:39 AM.

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding decimal in textbox and calculation

    I appreciate that you have tried to use the variable names I suggested but you've got things backwards. I would have hoped that it would be obvious (particularly given that I openly stated it) that 'pressureTextBox' would be used for the TextBox and 'pressure' would be used for the decimal.

    As for the issue, it is pretty much certain that the exception is NOT occurring when trying to parse "12.5" from a TextBox. That would work without issue. If you stop ignoring the information that the IDE is trying to provide and look at the error message then you would see what the actual problem is. It will be telling you that some String that doesn't look like a number (probably an empty String) cannot be converted to a decimal.

    The Parse method requires that the input be valid and yours clearly isn't. What you really should do is call TryParse instead. It will validate the data first and then only convert if it is valid. That will allow you to display a message to the user, telling them exactly what they did wrong and asking them to correct it. Make sure that you research the TryParse method before calling it. It will require a small restructure of your code but there are plenty of examples around, including in the MSDN documentation.

  10. #10
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Adding decimal in textbox and calculation

    Have a look at https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx you probably need the overload that takes NumberStyle.

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding decimal in textbox and calculation

    Quote Originally Posted by PlausiblyDamp View Post
    Have a look at https://msdn.microsoft.com/en-us/lib...v=vs.110).aspx you probably need the overload that takes NumberStyle.
    I don't think that that's going to help. A decimal point is allowed by default when parsing a String to a Decimal so you'd only use a NumberStyles value to exclude something that was usually allowed or to include something like the ability to parse hex or currency. The only reason that I can think of that Decimal.Parse would fail on "12.5" is if the system culture was using a comma as a decimal separator, but I would have assumed that the OP would already know if that was the case.

  12. #12
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: Adding decimal in textbox and calculation

    Quote Originally Posted by jmcilhinney View Post
    I don't think that that's going to help. A decimal point is allowed by default when parsing a String to a Decimal so you'd only use a NumberStyles value to exclude something that was usually allowed or to include something like the ability to parse hex or currency. The only reason that I can think of that Decimal.Parse would fail on "12.5" is if the system culture was using a comma as a decimal separator, but I would have assumed that the OP would already know if that was the case.
    Good point, I think I was confusing it failing when dealing with currency symbols - in my defence it was early and I hadn't had a coffee.

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    Code:
                double DoublePallow = 0.0;
                double DoubleVolume = 0.0;
                double DoublePxV = 0.0;
                string Pallow = TextBox.Text;
                string PVolume = VolumeTextBox.Text;
                string PxV = PxVTextBox.Text;
    
      if (double.TryParse(Pallow, out DoublePallow ))
      //if (txtPAllowTextBox < 0.5m)
    I do not understand how i can get the system to work with the for example "<" and ">=" symbols.
    It gives an error. while it works fine with integers.

  14. #14
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,296

    Re: Adding decimal in textbox and calculation

    Quote Originally Posted by rakker View Post
    Code:
                double DoublePallow = 0.0;
                double DoubleVolume = 0.0;
                double DoublePxV = 0.0;
                string Pallow = TextBox.Text;
                string PVolume = VolumeTextBox.Text;
                string PxV = PxVTextBox.Text;
    
      if (double.TryParse(Pallow, out DoublePallow ))
      //if (txtPAllowTextBox < 0.5m)
    I do not understand how i can get the system to work with the for example "<" and ">=" symbols.
    It gives an error. while it works fine with integers.
    Firstly, if there's an error then there's an error message. Keeping that error message a secret from those whom you want to help you is not a great strategy. Pointing out WHERE the error occurs would a good idea too, don't you think? There's only one line in there that uses a comparison operator and it's commented out, so the code you've actually shown us will generate no errors at all related to comparison operators. We are not psychic. We only know what you tell us so you have to tell us everything that is relevant and preferably clearly. Telling us that errors occur but not what they are or where they occur is not generally of much value and showing us code other than what you're actually running is also of little value.

    That said, if you have a Double variable named DoublePallow and you have populated that with the number that results from converting a String, it seems reasonable to you would then compare that DoublePallow variable to another Double value. If that's your number then there's no point comparing something else and there's no good reason to compare a Double to a literal Decimal when you can compare it to a literal Double. If what you are actually trying to achieve is check that a TextBox contains a valid Double value and that that Double value is less than 0.5 then doing so is very easy. You use Double.TryParse to validate and convert and then compare the converted value:
    csharp Code:
    1. if (double.TryParse(myTextBox.Text, out myDouble) && myDouble < 0.5)
    2. {
    3.     // A valid number has been entered and it is less then 0.5.
    4. }
    There is absolutely no difference between that and how you would properly do the equivalent for integers.

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    Got it to work.
    Code:
     double DoublePallow = 0000.0000;
                double DoubleVolume = 0000.0000;
                double DoublePxV = 0000.0000;
    
    
                if (radioButtonPxV.Checked == true)
                {
                    
                   
        
                         //Table 1
                    {
                            if (double.TryParse(PAllowTextBox.Text, out DoublePallow) && DoublePallow < 0.5)
                        
                        {
                            return "Table 1";
    
                        }
                        else
    //............
    Small related question. How could i return 2 results?
    Like example below.
    Code:
    //some declaration here? 
    //....int = ResTable;
    //....int = ResFigure;
    //
    
       if (double.TryParse(PAllowTextBox.Text, out DoublePallow) && DoublePallow < 0.5)
                        
                        {
                            return
    {
    ResTable = "Table 1";
    ResFigure = "Figure A";
    
                        }
    else
    }
    //..........

  16. #16
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    Re: Adding decimal in textbox and calculation

    Create a class with the properties you want, then you can create an instance of it, set the values, and return it.

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Dec 2003
    Location
    netherlands
    Posts
    163

    Re: Adding decimal in textbox and calculation

    Thanks that worked.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width