0

I have column in my database with decimal(18, 0) Data type. When I try to insert then I get "Wrong format". I do like this...:

decimal amountToWithdraw = Convert.ToDecimal(txtAmountToTransfer.Text);

Letsay if I write 25.89 then this givs me error message "wrong format" It's working with hole numbers, like 25 but not with dot 25.89

I use this eventHandler on Textbox:

private void txtAmountToTransfer_KeyPress(object sender, KeyPressEventArgs e)
        {
            char ch = e.KeyChar;
            if(ch == 46 && txtAmountToTransfer.Text.IndexOf('.') != -1)
            {
                e.Handled = true;
                return;
            }

            if(!Char.IsDigit(ch) && ch != 8 && ch != 46)
            {
                e.Handled = true;
            }
        }

It should be easy but I have tried with many methods but stil I dont get it to work. Thank you in advance

4
  • 1
    decimal.Parse or TryParse Commented Nov 20, 2018 at 8:15
  • 1
    duplicate: stackoverflow.com/questions/33134614/… Commented Nov 20, 2018 at 8:17
  • 1
    Well, if it is a user input, then decimal.TryParse will be a good idea (if you do not have masked textboxes or something). And it is not converting because of your current Culture settings, I believe comma , instead of dot . will work. Commented Nov 20, 2018 at 8:18
  • How to detect current culture separator: char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator); Commented Nov 20, 2018 at 8:27

2 Answers 2

0

Try the following approach:

private void txtAmountToTransfer_KeyPress(object sender, KeyPressEventArgs e)
{
  char ch = e.KeyChar;
  char decimalSeparatorChar = Convert.ToChar(Thread.CurrentThread.CurrentUICulture.NumberFormat.NumberDecimalSeparator);
  if(ch == decimalSeparatorChar && txtAmountToTransfer.Text.IndexOf(decimalSeparatorChar) != -1)
  {
     e.Handled = true;
     return;
  }

  if(!Char.IsDigit(ch) && ch != 8 && ch != decimalSeparatorChar)
  {
     e.Handled = true;
  }
}

Then decimal.Parse(txtAmountToTransfer.Text) will work fine. And make sure you are using correct decimal separator when you type in numbers.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you it is working but if I put 25,50 then in database did not insert 25,50 but 26. It's rounding. Is any way t oget 25,50 ... or if I put 30,80 then I get the same value in my database. I'am trying to create banking system. If everybody deposit with 25,50 and my code rounding to 26, then this bank will lose a lot of Money.. (it's just an example) so I wonder if there is any way to get the same value as I put in my textbox. As I mention in my question it's decimal(18, 0) Data type. Thank you again.
@HelenTekie it is rounded because of your db column type, change it to decimal (18,2). See the following post stackoverflow.com/questions/25190976/…
0

Try using

decimal amountToWithdraw = Convert.ToDecimal(txtAmountToTransfer.Text, CultureInfo.InvariantCulture);

4 Comments

This is nice until , is a decimal separator in your culture.
As mentioned in the question, he is trying to parse values like 25.89, so a comma is not the separator in this case.
@RameshVerma In OP's question you can't see his current culture settings and he's getting error while converting. So, probably comma is separator in this case.
@RameshVerma it just means that he is using incorrect separator. UI should work with UI culture and parse will work in that case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.