I have a NumericStepper in flex that must accept values between 0 and 999.99.
I tried setting the numericStepper as follows:
<s:NumericStepper id="numStepper" value="#{myValue}" maximum="999.99" snapInterval="0.01" stepSize="0.01" minimum="0"/>
and setting also a NumberValidator attached to it:
var nValidator:NumberValidator = new NumberValidator();
nValidator.source = numStepper;
nValidator.precision = 2;
numericStepper.maxChars=6;
nValidator.decimalSeparator=".";
The thing works but I would like also to directly limit the user input via keyboard in the numeric stepper, so that the user can't type things like "1.4567" but only 1.45.
So I want something to limit the integer and decimal part of the number according to my specifications:
max 3 chars integer part
"." decimal separator
max 2 chars precision
Maybe some regular expression can help?
Thanks
Have you tried...
nValidator.fractionalDigits = 2;
Related
I am trying to format a zero currency value as an empty string, so that when the currency value is 0.00 then an empty string gets displayed rather than $0.00.
This code is part of an ASP.Net app that will display currency value to end user.
I have used following code to achieve this goal.
Question : Is it possible to achieve this by just using {0:C} format string or another version of this format string instead of using if then else coding for this? If I use ###,###,###.## as the data format string then an empty string shows for zero currency value and also I get rid of the if then else coding but for non-zero values no currency symbol shows.
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
charValue = Nothing
Else
charValue = String.Format("{0:C}", CDec(currencyValue))
End If
UPDATE
I ended up using the following code, which is working fine. If is better than IIf because it does short-circuiting, which means that IIf will evaluate all expressions whether the condition is true or false but If will evaluate the first expression only if condition is true and evaluate the second expression only if condition is false.
Dim d As Decimal
Decimal.TryParse(currencyValue, d)
charValue = If(d = 0D, Nothing, String.Format("{0:C}", d))
I don't think there is a way using formatting to display an empty string.
But you can write it like:
charValue = If( currencyValue = 0D, "", currencyValue.ToString("C") )
using the If Operator (Visual Basic).
Also this is something I would not do:
If Double.Parse(Decimal.Parse(CDec(currencyValue))) = 0 Then
If currencyValue is Decimal:
If (currencyValue = 0D) Then
If currencyValue is Double:
If (currencyValue = 0R) Then
Also, if you are using a database and this is a Sql Server mind SQL Server Data Type Mappings
I don't think you can when using C or the other similar standard formats, since they are already defining a culture-specific format that will include a format for zero.
But if you specify your own custom format, you can specify three different formats separated by ;s, one each for positive numbers, negative numbers, and zero, respectively.
For example (giving an empty string for the zero format, resulting in blank zeroes):
charValue = String.Format("{0:#,##0.00;-#,##0.00;""""}", CDec(currencyValue))
And from what I can see, omitting the format for negative gives a default that matches the positive, whereas omitting the format for zero gives blank, which is what you're looking for, so this should be sufficient as well:
charValue = String.Format("{0:#,##0.00;;}", CDec(currencyValue))
(Using whichever custom format you wish.)
UPDATE: You can get the current currency symbol and manually put it into your custom format. IE:
Dim symbol = CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol
charValue = String.Format("{0}{1:#,##0.00;;}", symbol, CDec(currencyValue))
From the sound of it, though, I think I would actually recommend doing basically what you started with, maybe with an extension method.
<Extension>
Public Function ToCurrencyString(pValue As Decimal) As String
Return IIf(pValue = 0, "", pValue.ToString("C"))
End Function
Dim someValue As Decimal = 1.23
Console.WriteLine(someValue.ToCurrencyString())
This gives you exactly what you're looking for. The exact same format as C gives, but with blank zeroes.
i need to add a N number of brackets for the selected ListItem from list boxes for a given number from a variable. say ex. if i pass 5 from a variable it has to add the 5 brackets at the end of my list item values. can someone give me an idea
thanks
This is a bracket, isn't it? (
I don't know where exactly you have a problem with, but you can create a string with repeating characters with the constructor overload:
int bracketCount = 5;
string brackets = new string('(', bracketCount);
So if you want to add these brackets to the selected item in the ListBox:
listBox1.SelectedItem.Text += brackets;
I have a dropdownlist that has the value of two columns in it... One column is a number ranging from 5 characters long to 8 characters long then a space then the '|' character and another space, followed by a Description for the set of numbers.
An example:
12345678 | Description of Product
In order to pull the items for the dropdownlist into my database I need a to utilize a substring to pull the sequence of numbers out only.
Is it possible to write a substring to pull multiple character lengths? (Sometimes it may be 6 numbers, sometimes 5 numbers, sometimes 8, it would depend on what the user selected from the dropdownlist.)
Use a regular expression for this.
Assuming the number is at the start of the string, you can use the following:
^[0-9]+
Usage:
var theNumbers = RegEx.Match(myDropdownValue, "^[0-9]+").Value;
You could also use string.Split to get the parts separated by | if you know the first part is what you need and will always be numeric:
var theNumbers = myDropdownValue.Split("| ".ToCharArray(),
StringSplitOptions.RemoveEmptyEntries)[0];
Either of these approaches will result in a string. You can use int.Parse on the result in order to get an integer from it.
This is how I would do it
string str = "12345678 | Description of Product";
int delimiter;
delimiter = str.IndexOf("|") - 1;
string ID =str.substring(0, delimiter);
string desc = str.substring(delimiter + 1, str.length - 1);
Try using a regex to pull out the first match of a sequence of numbers of any length. The regex will look something like "^\d+" - starts with any number of decimal digits.
Instead of using substring, you should use Split function.
var words = phrase.Split(new string[] {" | "},
StringSplitOptions.RemoveEmptyEntries);
var number = word[0];
say I write the following javascript code:
var top = document.getElementById("SOMEDIVID").style.top;
Would the variable "top" end up storing the top value as a string or as a number? I want it to be a number.
It is a complex value - a number followed by a unit.
If a unit is missing, it is assumed to be pixels (px).
So, all of these are valid:
80px
80
50%
17em
use parseInt() then
var top = parseInt(document.getElementById("SOMEDIVID").style.top);
It would be a string. You can split off the integer with regular expression then a recast.
parseInt(top.match(/^\d+/)[0]); // integer only
This will tell you if it's a string or a number:
var type = typeof( top );
If it's a string, this will give you a number
var number = parseInt( top );
I would like to know the easiest way to format a string as accounting style. I know how to format as currency using {0:c} but there are some differences in accounting style, for example, all the dollar signs will line up as well as all the decimal points, and negatives are expressed in parenthesis rather than with a "-" minus sign. You can find a good example of the way i would like it in excel if you format the cells as "accounting" with 2 decimal places.
Ignoring your alignment requirements, you could use
number.ToString("€#,##0.00;(€#,##0.00);Zero")
to bracket negative numbers.
To align your numbers, you'd have to format without the currency symbol, and pad the formatted numbers yourself with spaces, using a fixed width font would make this job easier for you.
EDIT:
It seems String.Format is your friend:
String.Format("{0,15:#,##0.00 ;(#,##0.00);- }", number)
where 15 is the total width of the output, and you need to append this text to your currency symbol. (Again, this aligns in fixed width only)
There's no format string shortcut (the single-character ones with default rules) for handling accounting style formats (here's a cheat sheet with the available format strings) so you'll have to write a more specific one (like Patrick's answer) or your own parsing method.
The alignment requirements would be specific to how you're displaying them. I'm assuming you are using a table, in which case you're limited by what HTML supports, and it doesn't support accounting style alignments like Excel.
In this blog there were some various formats outlined and this one seemed to be close to what you were looking for:
int neg = -10;
int pos = 10;
// C or c (Currency): It represent how many decimal place of zeros to show.
String.Format("{0:C4}", pos); //"$10.0000"
String.Format("{0:C4}", neg); //"($10.0000)"
It doesn't handle the padding (you may have to fix that yourself), but it does have the proper parenthesis.
You could do something using a variation of Patricks method. This will handle formating and alignment assuming you know the upper bound of how large a value you are dealing with:
private static string OutputAsCur(decimal val)
{
string format = " #,##0.00 ; (#,##0.00);Zero";
string frmt = val.ToString(format);
return CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol + frmt.PadLeft(15, ' ');
}
Here's a simple example app to see it format:
static void Main(string[] args)
{
decimal d = 155.55m;
Console.WriteLine(OutputAsCur(d));
Console.WriteLine(OutputAsCur(d * -1));
Console.WriteLine(OutputAsCur(1002.32m));
Console.WriteLine(OutputAsCur(1002.32m * -1));
Console.ReadLine();
}
You can use a format string for String.Format to get what you're trying to accomplish. The only trick is that positive numbers, since they will not have a closing parenthesis mark, will have to incorporate a space at the end if they will be aligned with any negative numbers that will be in the column. The trick is to get that space into the string in a way that HTML will not ignore. I simply use the HTML entity which indicates a non-breaking space in HTML.
Here's sample code. First, in the aspx.
<table>
...
<tr>
<th scope="row" colspan="2">Total Revenue</th>
<td class="numeric total"><asp:Label runat="server" ID="TotalRevenueLabel" /></td>
</tr>
...
</table>
Now, the codebehind.
public const string kMoneyFormat = "#,#.00' ';(#,#.00);'-.-- '";
public void DataBind()
{
using (FinancialDataContext sql = new FinancialDataContext())
{
var periodQuery = from m in sql.Forecasts()
select m;
ForecastsResult periodData = periodQuery.Single();
decimal totalRevenue = period.Data.income_actual.Value + periodData.other_income.Value;
TotalRevenueLabel.Text = totalRevenue.ToString(kMoneyFormat);
}
}
I followed these steps for apply the "Accounting" format.
In a new Book on Excel, select a cell.
Insert data (i.e any number; for this example, add 80000).
Select (Accounting) NumberFormat as is shown in the screenshot #1:
Screenshot #1:
Select "More Number Formats".
Select "Custom".
Select any of the pre-defined formulas (see screenshot #2).
Screenshot #2:
In my case, this is the desired format for this number.
The negative side of this is that when you select the cell with the format applied on it, you wont see selected (Accounting) "in the DropDownList" Number Format.