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

Helen Tekie
  • 515
  • 1
  • 6
  • 23
  • 1
    decimal.Parse or TryParse – Access Denied Nov 20 '18 at 08:15
  • 1
    duplicate: https://stackoverflow.com/questions/33134614/how-to-convert-input-from-a-textbox-into-a-decimal-number – jazb Nov 20 '18 at 08: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. – SᴇM Nov 20 '18 at 08:18
  • How to detect current culture separator: `char a = Convert.ToChar(Thread.CurrentThread.CurrentCulture.NumberFormat.NumberDecimalSeparator);` – Access Denied Nov 20 '18 at 08:27

2 Answers2

0

Try using

decimal amountToWithdraw = Convert.ToDecimal(txtAmountToTransfer.Text, CultureInfo.InvariantCulture);
Ramesh Verma
  • 111
  • 1
  • 9
  • This is nice until `,` is a decimal separator in your culture. – Access Denied Nov 20 '18 at 08:24
  • As mentioned in the question, he is trying to parse values like 25.89, so a comma is not the separator in this case. – Ramesh Verma Nov 20 '18 at 08:27
  • @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. – SᴇM Nov 20 '18 at 08:28
  • @RameshVerma it just means that he is using incorrect separator. UI should work with UI culture and parse will work in that case. – Access Denied Nov 20 '18 at 08:28
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.

Access Denied
  • 8,723
  • 4
  • 42
  • 72
  • 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. – Helen Tekie Nov 20 '18 at 12:10
  • @HelenTekie it is rounded because of your db column type, change it to decimal (18,2). See the following post https://stackoverflow.com/questions/25190976/what-is-numeric18-0-in-sql-server-2008-r2 – Access Denied Nov 20 '18 at 12:46