How do you set text on Telerik Rad Numeric Textbox - c#

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);

Related

Format decimal to money c# aspx from sql

Hi I have this code in my .cs file and the output is 100449.00 but I want it to format into money like 100,449.00. This is my code to show the value in the label.
billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString();
billing.Text = "$" + ds.Tables[0].Rows[0]["Billing"].ToString("N");
Per this.
Edit: what's returned is an object which you need to cast to a decimal. Try:
((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N");
billing.Text = ds.Tables[0].Rows[0]["Billing"].ToString("c");
Coming from a DataTable requires us to convert to a non-nullable type before we format as text.
We actually have a few different ways to do the formatting. Your post has a hardcoded dollar sign preceding the value, in which case we can use either the F2 or N2 format strings to give us a decimal point with 2 places to the right and append that to the dollar sign you have in there:
billing.Text = "$" + ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("F2");
// 123456.7890 will display as $123456.78
billing.Text = "$" + ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("N2");
// 123456.7890 will display as $123,456.78
Another option is to use the C format which will add in the cultural specific currency symbol and numeric format (decimal points, commas) for us
billing.Text = ((decimal)ds.Tables[0].Rows[0]["Billing"]).ToString("C");
// 123456.7890 will display as
// $123,456.78 en-US
// 123 456.78€ fr-FR
// you could also add a second overload to the ToString to specify

How to display up to two digits at max but can show less?

How do I format a cell in a grid that is treated like text to be zero, one, or two digits depending on what the value is but to cut it to two digits if it has more?
If the value is 25 we display 25.
If the value is 26.3 we display 26.3.
If the value is 27.59 we display 27.59.
If the value is 28.124 we display 28.12.
If the value is 11.1111111 we display 11.11.
Does this make sense?
I'm using C#, MVC, and javascript/jquery.
If you do not want to round the value, you can do two different things:
//using String.Format()
string.Format("{0:#.##}", someValue)
//using ToString()
someValue.ToString("#.##")
Working fiddle here
If your value is not in a double/decimal then you can either manipulate the string by checking the index of the decimal point and trimming the string to 1 or 2 indexes to the right of that. However it might just be easier to parse the value into a new double then let the string formatting take it from there.
In C# you can user Math.Round function
decimal d = 28.1234;
Var a = Math.Round(d,2);
a will be 28.12
So because you said it's like a text you start with a string :
string text = "25.1234";
Then you can parse it to a double so you can round it :
double number = double.Parse(text);
Then you finish by rounding the value to the decimal you need and transforming it to a string :
text = Math.Round(number, 2).ToString();

Format String to Match Specific Pattern

I am trying to figure out how to format a string to a specific pattern.
When a user is entering their employee id number, they often get confused on what is expected from them. Because they are often told that their employee id is either a 5 digit or 4 digit number depending on when they were hired.
For example, my employee id number is E004033 but for most of our systems, I just have to enter 4033 and the system will find me.
We are trying to add this to one of our custom pages. Basically what I want to do is format a string to always look like E0XXXXX
So if they enter 4033 the script will convert it to E004033, if they enter something like 0851 it will convert it to E000851 or if they enter 11027 it will convert it to E011027
Is there a way basically add padding zeros and a leading E if they are missing from the users input?
You can simply:
var formattedId = "E" + id.PadLeft(6, '0');
To remove an existing leading E(s)
var text = "E" + val.TrimStart(new[] {'E'}).PadLeft(6, '0');
Make sure the user's input is an integer, then format to 6 spaces using String.Format.
int parsedId;
bool ok = int.TryParse(id, out parsedId);
if (ok)
{
return String.Format("E{0:000000}", parsedId);
}

How to convert currency format label to double for calculation purpose

I have a label (lblAmountTendered) which contains Currency String in it. I would like to convert it into double to perform some calculation. However, it shown an Error Message: Input string was not in a correct format in the first statement.
Here is my code:
double balance = double.Parse(amount) - double.Parse(lblAmountTendered.Text.ToString());
lblBalanceDue.Text = balance.ToString("c2",CultureInfo.CreateSpecificCulture("en-MY"));
For Example:
lblAmountTendered = RM 15
I want to retrieve the value of it (15) for calculation.
Looking forward for the solution. I appreciate for your help! :)
PROBLEM SOLVED
lblAmountTendered.Text.ToString().Remove(0,3)
Remove(0,3) helps us to remove 'RM' from 'RM 15', so we can convert it to double or float easily as shown in below:
float.Parse(lblAmountTendered.Text.ToString().Remove(0, 3))
Here you first need to retrieve the numeric value from a string using below regex:
string resultNum = Regex.Match(lblAmountTendered.Text, #"\d+").Value;
and then use resultNum in your code like-
double balance = double.Parse(amount.ToString()) - double.Parse(resultNum);
lblBalanceDue.Text = balance.ToString("c2",CultureInfo.CreateSpecificCulture("en-MY"));

Decimal digit problem

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;
}

Categories