Decimal digit problem - c#

On Asp.Net, in my textbox, i do not want to enter after zero three numbers.
For example if you enter textbox 0.222 it should be 0.22. After 0.22 you should not enter anything. How can i make this?
Thanks

Use a Masked TextBox.

I think it is better to use an Expression instead of Masked Textbox.
With a simple RegularExpressionValidator you are able to do this, just drag one in your worksheet and then set the TargetControl of it to your Textbox, then set its value to ^\ddd.

Using javascript add an attribute onkeyup='formatNumber(this)' on the tag.
function formatNumber(textBox) {
var current = textBox.value;
var output = "";
//find '000' in current
//delete what you want
textBox.value = output;
}

Related

Display floating points in f3 format in RDLC TABLIX

In my report, I have a tablix. Now I want to show the numeric data in f3 format. I have set text box property of the tablix column to number and with three decimal points.
Now when the data is e.g. 12.120 then it shows me 12.12 instead of 12.120.
And I want to show when the data is like 12 to 12.000.
How to do it?
double d = 12.20;
Console.WriteLine(d.ToString("0.000", new CultureInfo("en-US", false)));
First off, here's your reference material.
You can use those format strings, or the placeholder style.
Examples:
Double value = 12.20D;
String str1 = String.Format("{0:F3}", value);
String str2 = value.ToString("F3");
String str3 = value.ToString("0.000");
To make this work with your TextBox, however, you will need to pass the entered values through a routine that applies said formatting.
Using the TextBox's Validated method is a good choice because it is fired after editing is complete, and not while the user is still typing (like TextChanged would):
private void textBox1_Validated(Object sender, EventArgs e)
{
Double dbl;
if (!String.IsNullOrWhiteSpace(textBox1.Text) &&
Double.TryParse(textBox1.Text, out dbl))
{
//Replace with your formatting of choice...
textBox1.Text = String.Format("{0:F3}", dbl);
}
}
You have to set TextBox.Format property to f3.

How to write half-space in c#

How can I write the half-space character in c# in a Textbox (not Console.Writleline statement) ? Is there a specific char code for it?
Unicode THINSPACE U+2009 is a perfectly ordinary character, try:
TextBox.Text += '\u2009';
I don't know what you mean by half-space, but Char.Parse() will probably do what you want. Please get a list of Unicode spaces
Then do this (example is for 1/2 en space; remove all plus signs):
string UnicodeString = "\u2000"; // U+2000
TextBox.Text = Char.Parse(UnicodeString);
However, you probably don't need Char.Parse() for this. You could simply do this:
string halfSpace = "\u2000"; // So you can reuse it
TextBox.Text = halfSpace;
UPDATE: If you want to add it to the TextBox (+=), rather than replacing the whole line (=), use this:
TextBox.Text += halfSpace

Check if a NumericUpDown has no value

How can I check if a user has left a NumericUpDown control empty? I have seen on another question to check the 'Text' property but there is no text property to check against.
You can check against the Text property, although it is hidden from the designer and IntelliSense.
Just try this sample. It does work:
if (string.IsNullOrEmpty(control.Text))
{
// no value
}
You can use ,
if(NumericUpDown.Text == "")
{
// If the value in the numeric updown is an empty string, replace with 0.
NumericUpDown.Text = "0";
}

On inserting to a database with predefined value C#

OK I have a text box with a set value, I only want this set value sent if the text box is not filled in on completion of form.
code
dontaion_euro.Text = "99.00";
problem is when I click send it only sends the data 99.00 I only want this sent if blank.
how could I achieve this?
Step1 : you need to Trim the Textbox value to remove the white spaces.
Step2 : you can compare the Textbox value with Empty String to check whether Textbox value is Empty or not
Try this:
if(donation_euro.Text.Trim().Equals(""))
{
donation_euro.Text="99.00";
}
If I understand correctly, you can use String.IsNullOrEmpty method like;
Indicates whether the specified string is null or an Empty string.
if(string.IsNullOrEmpty(dontaion_euro.Text))
{
dontaion_euro.Text = "99.00";
}
else
{
//Your textbox is not null or empty
}

How do you set text on Telerik Rad Numeric Textbox

I have come across a lot of threads on how to format the text in the box for input, but I believe this question is different enough to be on its own because I cannot find an answer.
I need to format text after the user inputs a value in the rad numeric textbox.
The user is required to enter a number between .001 and .999. After this input, the returned value must be the number in percent form followed by a percent sign
ie:
if the user enters .500, then the box must return 50.000%
This seems simple in theory, but I keep getting this error
Text property cannot be set. Input string was not in a correct format
Here is the code I am using.
C#
double pct = rntb.Value.GetValueOrDefault();
string result = pct.ToString("P3");
rntb.Text = result;
ASP
<telerik:RadNumericTextBox ID="rntb" runat="server" Type="Percent" Value="1" OnTextChanged="rntb_TextChanged">
I suggest you to use RadMaskedTextBox
<telerik:RadMaskedTextBox ID="RadMaskedTextBox1" runat="server" Mask=".###"
Width="100%" PromptChar="_" />
Get your value using RadMaskedTextBox1.Text; , it will return RadMaskedTextBox's text (number only) without .(dot) .
For example
.001 => 001
.500 => 500
.999 => 999
So , you have to convert likes
string yourPercentage = (Double.Parse(RadMaskedTextBox1.Text)/ 10).ToString() + "%";
Edit
You can change and test this method ,
RadMaskedTextBox1.Text;
RadMaskedTextBox1.TextWithLiterals;
RadMaskedTextBox1.TextWithPrompt;
RadMaskedTextBox1.TextWithPromptAndLiterals;
Good Luck :)
Try something like this:
double pct = rntb.Value.GetValueOrDefault();
rntb.Text = String.Format("{0:P3}.", pct);

Categories