Textbox that accepts upto 4 digit decimals - c#

I'm looking to accept digits and the decimal point for forms, using WPF application.
I am new to WPF
I want a textbox that takes only numeric values, in the format
12.3456
that is, 2 places before decimal point and 4 places after it.

Too bad TextBox has no Property to set the DataType of the field and the amount of Decimal Places it can hold, had to make a UserControl that does exactly what you want.
Offtopic aside, use Regex, I usually use this website to build them.

Related

In Unity3D, remove all but a set amount of decimals from a number

For a text display in Unity3D. Wht would I do if I wanted to remove all but say 2 decimals from a float? To clarify what I want to do, please see the example below:
Say I have the floats:
1.25013
1.9012
1.029
Now, in Unity what should I do if I want to shorten them so only two decimals remain i.e returning 1.25, 1.90 and 1.02 before displaying the string such as on a UI.Text ?
I have looked into string formatting and stuff like Math.Floor without success.
As said in the comments, if it is just for display the correct way to solve this problem is pass in a numeric format to a .ToString call.
To get the format you described you would want the format string f2, that would create a fixed-point number with two decimal points that will look like 1234.50
UI.Text = someNumber.ToString("f2");
If you would like a , in the number when you have more than 3 digits to the left of the decimal place like 1,234.50 use n2 instead of f2
UI.Text = someNumber.ToString("n2");

iOS App on IPad removing dot decimal value

I am making a iOS app in xamarin. I am working with decimal values & everything works great on the iPhone. But when I test the app on a iPad, 1 (not all) decimal value get's misinterpreted.
For exemple there is a string value : 1200.00.
I am Parsing this value :
if (Decimal.TryParse (vehicle.offer_price, out result)) {
but I am getting back : 120000 on the iPad & 1200.00 on the iPhone?
What's up with this?
Kind regards
The region settings on each device is different.
One device is set to a region format where the dot is a thousand separator, the other where it's a decimal separator.
I think you may be confusing the internal implementation of Decimal with how it is presented. Floating point types (decimal, double etc) are implemented as binary data structures, which can be presented to us in an number of different formats (depending on where they are being displayed), which may or may not include the significant digits after the decimal point. Normally in the UI it is up to you the programmer to decide what display format to use (e.g., how many digits after the decimal point).
Compare this to the debugger - here the display format has been chosen by the implementors of the IDE, who may or may not include digits after the decimal point (but it would be a bit odd if they did not display any).
The important thing is, whatever the display format, this does not change the actual underlying value in the Decimal.

How to validate a number in asp.net?

I have a TextBox that the user need to fill with data a number between 0-90
with 3 decimal places (ex. 0.123 , 1.456 , 1.3 , 45)
i need to validate it.
i can use some validation controls.
RangeValidator with type double, min 0 , max 90
RegularExpressionValidator
what is better?
Do it with progressive enhancement in mind.
First make a non-javascript version where the textbox is validated server-side.
Second make a nice javascript validation using a RangeValidator.
Keep the RegEx validator's for phone numbers (1800 Plumbing) , emails, fax's & etc.
Is this 3 decimal places exactly or it can vary? If it’s 3 exactly I’d go with regex validator because I’m not sure range validator can validate for exact number of decimal places.
Also you need to make sure these are validated on the server side as well.

Convert text data into percentage in C#

I am getting data into a text field and I need to display it as a percentage. Is there a function to perform this?
Ex: in my column I have "0.5", "0.1","0.2","0.25" etc., which needs to be displayed as
50%,10%,20%,25% etc., What is the best way to do it?
You should do this in two phases:
Parse the text as a number so you've got the value as your "real" type. (As a general rule, parse from text as early as you can, and format to a string as late as you can... operations between the two will be a lot simpler using the natural type.)
Format the number as a percentage using the standard numeric format string for percentage
So:
decimal percentage = decimal.Parse(input);
string output = percentage.ToString("p0");
Notes:
You should consider both input and output culture; are you always expecting to use "." as the decimal separator, for example?
Use decimal rather than double to exactly represent the value in the text (for example, the text could have "0.1" but double can't hold a value of exactly 0.1)
You can add things like desired precision to the formatting; see the linked docs for details; the example gives just an integer percentage, for example
Easiest would be to parse it (must be a double) then convert it back to a string, formatting it as a percentage.
var percentageString = double.Parse(doubleString).ToString("p1");
Now, some of you hoity-toity types may say that decimal is the correct type to use in this case.
Well, yes, if you need an additional 12-13 digits of precision.
However, most of us real folk (and I'm all about keeping it real) are fine with double's 15-16 digits of precision.
The real choice is whether or not your code is using doubles or decimals in the first place. If you are using doubles in your code, just stick with doubles. If decimals, stick to decimals. What you definitely do want to avoid is having to convert between the two any more than is absolutely necessary, as there be dragons. And unexpected runtime bugs that can corrupt your data. But mostly dragons.

In C#, how can I display only a certain number of digits?

In my application, I have a TextBlock that I display a Double number in after the user presses a button. This number can be a very small decimal or a very large number needing exponential notation (i.e. 3.43e12). The problem is, the program prints so many digits that it overflows my TextBlock and the user can't see all the valid information.
So how can I restrict the Double to print so to not overflow the TextBlock?
The code I am using to set the text is:
theTextBox.Text = (split * input).ToString();
EDIT: Someone asked for specific examples, so I thought I would clarify something. I basically want the string to never be longer than, say, 10 characters. That way it will fit in the TextBlock. I guess the trick is, when should those 10 characters be decimal places, whole numbers, or scientific notation that is the trick...
Use Double.ToString(String), giving an appropriate format specifier, as described at http://msdn.microsoft.com/en-us/library/kfsatb94.aspx.
Have a looksee here
http://msdn.microsoft.com/en-us/library/0c899ak8.aspx
You can put your format string in as a param to the ToString method

Categories

Resources