Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

How to make thousand separator and decimals in textbox in C#

Posted on 2016-09-27
13
73 Views
Last Modified: 2016-09-27
I'd like to input numbers in a textbox and format it with a thousand separator and two decimals.

I've tried the Masked Edit with a lot of different masks, but I just can't get it to work.

I found something interesting but I have a problem when I put some numbers in the textbox, the cursor comes always to the left ...
Here is the code:
 double text = double.Parse(txt.Text);
                CultureInfo culture = CultureInfo.CreateSpecificCulture("fr-FR");
                

                txt.Text = text.ToString("N0", culture);  

Open in new window

But when I insert in the textbox for example 2500 I got 02 500 while I want 2 500.
Any suggestions ?
Thank you.
0
Comment
Question by:SniperCode Sheva
  • 7
  • 5
13 Comments
 
LVL 70

Expert Comment

by:Éric Moreau
ID: 41818130
can you use 3rd party components like Telerik/DevExpress/Infragistics/... who are doing a much better job to handle this?
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818135
You can use the currency format of ToString()

Example:
Fiddle Demo Here

using System;
					
public class Program
{
	public static void Main()
	{
		decimal myNum = 2500.01m;
		string myString = myNum.ToString("C");
		Console.WriteLine(myString);
		
		double myDouble = 2500.01;
		string myString2 = myDouble.ToString("C");
		Console.WriteLine(myString2);
	}
}

Open in new window

0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818150
Or, if you don't want currency and just want the thousands separator with 2 decimal places:
using System;
using System.Globalization;
					
public class Program
{
	public static void Main()
	{
		double myDouble2 = 2500;
		string myString3 = myDouble2.ToString("N",new CultureInfo("en-US"));
		Console.WriteLine(myString3);
	}
}

Open in new window

0
MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

 

Author Comment

by:SniperCode Sheva
ID: 41818151
Hello Zephir, I don't know if you understood my question... I need to seperate the thousand with spaces and when I put some numbers in the textbox, the cursor comes always to the left ...
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818160
You could separate the thousands with spaces by doing a string replace, and replace the comma with a space.

From my last example above, just do:

var myStringWithSpaces = myString3.Replace(",", " ");

Open in new window


To control where the cursor is positioned in the textbox, you will need client side code (i.e.  JavaScript / jQuery), not C#.
0
 

Author Comment

by:SniperCode Sheva
ID: 41818190
Is it possible to put Jquery code in C# ?
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818235
No.  C# is server side code that runs before the page renders.  jQuery is client side code that runs after the server side code is done.  And positioning a cursor requires that the page is loaded, which means it must be done in client side code.
0
 

Author Comment

by:SniperCode Sheva
ID: 41818237
So, there is no a solution ? because when I insert in my textbox 2500 I got 02 500 and if I continue to insert the numbers will be added after the 2 like "22 500"
0
 
LVL 43

Accepted Solution

by:
zephyr_hex (Megan) earned 500 total points
ID: 41818266
Are you inserting text from code behind or actually in the HTML textbox?

And if you use the code I gave you above and assigned myStringWithSpaces to your textbox from codebehind, the textbox would show 2 500.00 or 2 500 00, depending on whether you also replace the decimal with a space.

using System;
using System.Globalization;
					
public class Program
{
	public static void Main()
	{
		double myDouble2 = 2500;
		string myString3 = myDouble2.ToString("N",new CultureInfo("en-US"));
		Console.WriteLine(myString3);
		
		var myStringWithSpaces = myString3.Replace(",", " ");
		Console.WriteLine(myStringWithSpaces);
		
		var myStringWithSpaces2 = myStringWithSpaces.Replace(".", " ");
		Console.WriteLine(myStringWithSpaces2);
	}
}

Open in new window


Updated Fiddle Demo.
0
 

Author Comment

by:SniperCode Sheva
ID: 41818280
I have to mention that I am using textbox !
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818290
I'm confused when you say you want the cursor positioned in the left of the textbox.  That suggests to me that you're talking about the textbox once it's rendered on the client side, and that would require JavaScript or jQuery to position the cursor.  You can not position a cursor from C# because the HTML and cursor don't exist at that point.

If you're talking about assigning your string to a textbox from C#, that's possible.  Just do:
TextBox1.Text = myStringWithSpaces2;
0
 

Author Comment

by:SniperCode Sheva
ID: 41818307
I don't want to put the cursor in the left side... But when I insert in the textbox the cursor automatically go in the left side and I put this code on the key press event...
0
 
LVL 43

Expert Comment

by:zephyr_hex (Megan)
ID: 41818348
If you're using key press event, you really should use jQuery for this.  Otherwise, you're making the user wait for a page lifecycle with every key press.

Here's an example with jQuery:

HTML
<input type="text" id="myTextBox">
<span style="cursor: pointer;">Click here when done</span>

Open in new window


jQuery
$('#myTextBox').blur(function() {
  var val = parseFloat($(this).val()).toLocaleString(undefined, {
    minimumFractionDigits: 2
  });
  var newVal = val.replace(",", " ").replace(".", " ")
  $(this).val(newVal);
});

Open in new window


Example usage:  enter 123456 into the textbox, and then click over to the side.
Result:  123 456 00

Here is a Fiddle Demo.

If that doesn't do it for you, then I'm sorry, but I don't understand what you're trying to accomplish so I'll leave it to someone else to answer.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question