Trailing zero on decimal - asp.net

I have a string which looks like this 512.3 is there a way to add a trailing zero so it looks like this 512.30
Just to clarify (sorry I didn't know there where different ways etc.)
My string is an amount which is passed so changes all the time I only need the trailing zero on amounts like 512.3, 512.4,512.5 etc. as some of my amounts will pass values like 512.33 and 512.44 and so on
Thanks
Jamie

float.Parse("512.3").ToString("0.00");
It would give the number with two decimal digits.

You're going to want to use String.Format or some derivation thereof, and the format string will look like
myString = String.Format("{0:F2}",myObject);
Also note that format strings can be used in the .ToString("F2") method (notice I included the format string F2 inside there already.
See the MSDN links above for a more thorough and definitive explanation.

If it's VB.NET it seems like the simplest solution would be:
FormatNumber("512.3", 2)
Which would return 512.30

Format(5.12, "0.00") this will format it as two decimals.

You'll want to use String.Format
decimal your_number = 1452.66m;
string str = String.Format("{0:C}", your_number);

Single.Parse("512.3").ToString("0.00") ' VB Version

Related

Unix Add double quotes in string

I need to declare a string like below
ext = "EXT
But I am not able to achieve it.
I tried below code and it is not worked
ext = '"EXT'
above code giving null only
Please help me
Thanks in advance
You have given extra space before and after the =
The second way which you tried and suggested in other answers will work:
ext='"EXT'
You may try using escape character "" and try something like this :
ext="\"EXT"
ext='"EXT' should work (at least in bash).
You have space between ext and equal sign. That could be a problem.

Need regular expression for validating date in dd-MMM-yyyy format

I am not expert in writing regular expressions so need your help. I want to validate date in "dd-MMM-yyyy" format i.e. 07-Jun-2012. I am using RegularExpressionValidator in asp.net.
Can anybody help me out providing the expression?
Thanks for sharing your time.
Using a DatePicker is probably the best approach. However, since that's not what you asked, here's an option (although it's case sensitive):
^(([0-9])|([0-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
In addition, here's a place you can easily test Regular Expressions: http://www.regular-expressions.info/javascriptexample.html
Regex without leading zero in day.
^\d{1,2}-[a-zA-Z]{3}-\d{4}$
Update Regex with leading zero in day.
^\d{2}-[a-zA-Z]{3}-\d{4}$
It's not regex, but you can use build in DateTime.TryParseExact function to validate your datetime string
DateTime dateTime;
string toValidate = "01-Feb-2000";
bool isStringValid = DateTime.TryParseExact(
toValidate,
"dd-MMM-yyyy",
CultureInfo.InvariantCulture,
DateTimeStyles.None,
out dateTime);
The accepted solution allows '00' as the day, so here is a fix for that:
^(([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)\-\d{4}$
Notes/Exceptions:
1.Be aware of case sensitivity issues. Eg. 'DEC' will not pass while 'Dec' will pass. You may want to convert the regex string and test string to lowercase before testing (if your application allows).
2.This will not catch days that don't exist, like Feb 30th, June 31st, etc.
"\d{4}\d{2}\d{2}|\d{2}/\d{2}/\d{4}|\d{2}.\d{2}.\d{4}|\d{2}\-(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)|(Jan|Feb|Mar|Apr|May|Jun|Jul|Aug|Sep|Oct|Nov|Dec)-\d{2}\-\d{4}|\d{4}-\d{2}-\d{2}"
These format is mm.dd.yyyy, d-MMM, mm.dd.yyyy
Yet another idea would be to try this (similar to user1441894's idea):
var x = DateTime.Parse("30-Feb").GetDateTimeFormats();
I learned to use this yesterday (for a different purpose). So try-catch this statement to deal with validity/invalidity of the date :)
"^(([1-9]|0[1-9]|1[0-9]|2[1-9]|3[0-1])[-]([JAN|FEB|MAR|APR|MAY|JUN|JULY|AUG|SEP|OCT|NOV|DEC])[-](d{4}))$"
using System.Text.RegularExpressions
private void fnValidateDateFormat(string strStartDate,string strEndDate)
{
Regex regexDt = new Regex("(^(((([1-9])|([0][1-9])|([1-2][0-9])|(30))\\-([A,a][P,p][R,r]|[J,j][U,u][N,n]|[S,s][E,e][P,p]|[N,n][O,o][V,v]))|((([1-9])|([0][1-9])|([1-2][0-9])|([3][0-1]))\\-([J,j][A,a][N,n]|[M,m][A,a][R,r]|[M,m][A,a][Y,y]|[J,j][U,u][L,l]|[A,a][U,u][G,g]|[O,o][C,c][T,t]|[D,d][E,e][C,c])))\\-[0-9]{4}$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-8]))\\-([F,f][E,e][B,b])\\-[0-9]{2}(([02468][1235679])|([13579][01345789]))$)|(^(([1-9])|([0][1-9])|([1][0-9])|([2][0-9]))\\-([F,f][E,e][B,b])\\-[0-9]{2}(([02468][048])|([13579][26]))$)");
Match mtStartDt = Regex.Match(strStartDate,regexDt.ToString());
Match mtEndDt = Regex.Match(strEndDate,regexDt.ToString());
if (mtStartDt.Success && mtEndDt.Success)
{
//piece of code
}
}

Format string at runtime

I'm creating a html table at runtime (no probs there), and I would like to be able to format the content in the cells at runtime by passing in a format string (ie currencies, decimals, decimal places etc)
for example, i want to achieve something like this but to be able to pass in the format of the string with code as a string, ie "{0:c}" or "#,###,###"
ideally to be able to pass it into the ToString() method ( i can't do that but was wondering if there could be a clever way to achieve this?)
tblCell.Text = dt.Rows[i][j].ToString(#.##);
tblCell.Text = String.Format("{0:c}", dt.Rows[i][j])
and
tblCell.Text = String.Format("{0:#.##}", dt.Rows[i][j])
should work.
You can supply format strings to the columns in your GridView by setting the DataFormatString property of the column to something like this: “{0:d}”.
Have a look at:
http://www.cheat-sheets.org/saved-copy/msnet-formatting-strings.pdf
I always use this cheat sheet to find out things like these as the number of possibilities is simply to big to remember them all

Regex for ASP.NET url rewrite

Sample text =
legacycard.ashx?save=false&iNo=3&No=555
Sample pattern =
^legacycard.ashx(.*)No=(\d+)
Want to grab group #2 value of "555" (the value of "No=" in the sample text)
In Expresso, this works, but in ASP.NET UrlRewrite, it is not catching.
Am I missing something?
Thanks!
I would do something along these lines:
^legacycard.ashx\?(?:.+&)*No=(\d+)
The \? will escape the question mark that normally separates the URL and the parameters, then you make sure that it will capture every parameter key/value pair (anything that ends on &) before the parameter you actually care about. Using ?: lets you specify that the set of brackets is non capturing (I'm assuming you won't need any of the data, has the potential to slightly speeds up your regex) and leaves you just 555 captured. The added benefit of this approach is that it'll work regardless of parameter order.
Just use this regex:
^legacycard\.ashx\?save=(false|true)&iNo=(?<ino>\d+)&No=(?<no>\d+)
Then Regex Replace with
${no}
Looks fine to me, your regex should match the entire string
legacycard.ashx?save=false&iNo=3&No=555
not sure why you have groups, but groups should also return
?save=false&iNo=3&
and
555
For good measure you should know that the . in legacycard.ashx is also interpreted by regex and you would normally escape it, in this case it dosen't matter because a single dot matches everything, also a dot. :)
Try this
^legacycard.ashx(\?No=|.*?&No=)(\d+)
this should work.

Regular Expression Pattern Error C#

When I have an Expression declared like
someText = Regex.Replace(someText, #"/*.*?*/", "");
The Error Says
System.ArgumentException: par"/*.*?*/"
parsing - Nested quantifier *.
How to rewrite the code to avoid this error?
It doesn't like that you have this: ?*
This basically translates to "zero or one of the previous expression zero or more times" which seems a little odd. I'm pretty sure that's the same thing as saying "zero or more times". Can you explain what you are trying to do in more detail?
I suspect that if you change your regex to this it will do what you want:
(/*.*)*/
Maybe what is needed is a verbal description or sample of what you are trying to match. Here is my guess of what you want. I just added an escape for the "?" character.
string someText = Regex.Replace(someText, #"/*.*\?*/", "");
It appears you're trying to parse /* */ style comments. You may wish to try a regex like:
someText = Regex.Replace(someText, #"/\*.*\*/", "");
This ensures that your * are escaped as actual characters.
Here is a good site to test your regular expressions without much trouble:
http://www.regular-expressions.info/javascriptexample.html
I hope this will help a bit.

Resources