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

  • Home
  • SEARCH
  • 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 9152663
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T12:06:40+00:00 2026-06-17T12:06:40+00:00

With this code I can modify decimal place, using numericUpDown . This code works

  • 0

With this code I can modify decimal place, using numericUpDown. This code works if I initialize myDecimal variable. But I need modify decimal place to a value typed in textbox.

In other word myDecimal = tbxConvertito.Text. But in this case, code not work.

Check screenshot in this page please: change decimal place in textbox using numericUpDown

public partial class Form1 : Form
{
    public decimal myDecimal = 3755.25012345M;

    public Form1()
    {
        InitializeComponent();
        tbxConvertito.Text = myDecimal.ToString();
        numericUpDown1_ValueChanged(this, EventArgs.Empty);
    }

    private void numericUpDown1_ValueChanged(object sender, EventArgs e)
    {          
        int decimalPlace = (int)numericUpDown1.Value;
        string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });
        string tmp = string.Empty;
        if (decimalPlace <= numbers[1].Length)
        {
            tmp = "," + numbers[1].Substring(0, decimalPlace);

            if (tmp.EndsWith(","))
                tmp = string.Empty;
        }
        else
            tmp = "," + numbers[1];

        tbxConvertito.Text = numbers[0] + tmp;
    }
}
  • 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-06-17T12:06:42+00:00Added an answer on June 17, 2026 at 12:06 pm

    You can split property Text from tbxConvertito.

    Also when text will be changed in TextBox you must invoke numericUpDown1_ValueChanged to restrict settings in NumericUpDown.

     public partial class Form1 : Form
        {
            public decimal myDecimal = 3755.25012345M;
    
            public Form1()
            {
                InitializeComponent();
                tbxConvertito.Text = myDecimal.ToString();                        
                numericUpDown1_ValueChanged(this, EventArgs.Empty);
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {          
                int decimalPlace = (int)numericUpDown1.Value;                        
    
                string[] numbers = tbxConvertito.Text.Split(new char[] { '.', ',' });
    
                string tmp = string.Empty;
                if (numbers.Length != 1)
                {
                    if (decimalPlace <= numbers[1].Length)
                    {
                        tmp = "," + numbers[1].Substring(0, decimalPlace);
    
                        if (tmp.EndsWith(","))
                            tmp = string.Empty;
                    }
                    else
                        tmp = "," + numbers[1];                
                }
                tbxConvertito.Text = numbers[0] + tmp; 
                tbxConvertito.Select(tbxConvertito.Text.Length, 0);
            }
    
            private void tbxConvertito_TextChanged(object sender, EventArgs e)
            {
                numericUpDown1_ValueChanged(this, EventArgs.Empty);
                decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
            }
        }
    

    Without lose data:

    public partial class Form1 : Form
        {
            public decimal myDecimal = 0;
    
            public Form1()
            {
                InitializeComponent();
                // init value
                tbxConvertito.Text = myDecimal.ToString();
                numericUpDown1_ValueChanged(this, EventArgs.Empty);
            }
    
            private void numericUpDown1_ValueChanged(object sender, EventArgs e)
            {
                int decimalPlace = (int)numericUpDown1.Value;
    
                string[] numbers = myDecimal.ToString().Split(new char[] { '.', ',' });
    
                string tmp = string.Empty;
                if (numbers.Length != 1)
                {
                    if (decimalPlace <= numbers[1].Length)
                    {
                        tmp = "," + numbers[1].Substring(0, decimalPlace);
    
                        if (tmp.EndsWith(","))
                            tmp = string.Empty;
                    }
                    else
                        tmp = "," + numbers[1];
                }
                tbxConvertito.Text = numbers[0] + tmp;
    
                tbxConvertito.Select(tbxConvertito.Text.Length, 0);
            }
    
            private void tbxConvertito_TextChanged(object sender, EventArgs e)
            {
                if (keyValue == 188) return;
                if (keyPressed)
                {
                    string stringValue = tbxConvertito.Text;
                    if ((stringValue.Contains(',') && stringValue.Split(new char[] { ',' })[1].Length <= (int)numericUpDown1.Value) || !stringValue.Contains(','))
                        decimal.TryParse(tbxConvertito.Text.Replace(',', '.'), out myDecimal);
                    keyPressed = false;
                }
                numericUpDown1_ValueChanged(this, EventArgs.Empty);
                Console.WriteLine("Displayed value: {0}", tbxConvertito.Text);
                Console.WriteLine("Actual value: {0}", myDecimal);
            }
    
            bool keyPressed = false;
            int keyValue;
    
            private void tbxConvertito_KeyDown(object sender, KeyEventArgs e)
            {
                keyValue = e.KeyValue;
                keyPressed = true;
            }
        }
    

    Scenarios:

    1. [Decimal Places = 0] you can type e.g. [1], [1234], [12345]
    2. [Decimal Places = 1] you can type e.g. [1,1], [1234], [12345,3]
      if
      you change value in NumericUpDown to 0 displayed value will be [1],
      [1234], [12345]
      if you change value in NumericUpDown to 1 again
      displayed value will be [1,1], [1234], [12345,3]
    3. [Decimal Places = 1] the same situation as for step 2

    Now we don’t lose any data. We update our original data only if user will type something in our textbox.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

With this code we can create a simple word document but I need to
Is there anyway I can modify this code example #include <stdlib.h> #include <iostream> class
Looking at this jQuery example, how can I modify the code so that it
With this code I can download the file but I should know the file
Below is the CSS code of my table display. How can I modify this
Pls help me modify this code but I would like to keep it 90%
I have this code that I can't modify, and I want to add a
How can I modify this code to add a 0 before any digits lower
How can I modify this code for the second array's values to start with
How can I modify this code to improve protection against sql injection and other

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.