Formatting a number in gridview - asp.net

i have a gridview that shows some indicators.one of the numbers is a computed percentage it looks like this "33.33333333333333333333333333333333333333" i tried to set the DataFormatString property of the column to several different formats such as "{0:P}" ,"{0:D}" and "{0:##.##}" but nothing worked for me to show it like that "33.34". Any ideas?

What happens if you use "{0:F2}"?

Try entering just the format string without braces and the index,eg. P, D or ##.### . The braces and index are used by String.Format, where there are more than one format parameters. ToString methods (e.g. int.ToString) and control format parameters use only the format string without braces

Related

Find and Replace a character in a string in VB.NET

I have a certain input string in this form: "[3] [4] at [5]"
From the following datatable, I need to replace the text on the datatable corresponding to the column index inside the bracket.
The output should be: "15A Circuit Breaker #348901836 at 19-Afalcon St. Capitol Subdivision"
Right now, I am using Regex.Replace() method but it searches for a particular pattern. My problem is the integers (corresponding the column index) enclosed inside the brackets is dynamic.
What would be the best way to achieve this?
String.Format should do it for you. Both the format string and the arguments can be easily implemented dynamically.
https://msdn.microsoft.com/en-us/library/system.string.format.aspx
All of the syntax, and a decent example of what you wish to do are on the linked page.

Number format with Eval in ASP.NET

I would like to format this kind of number:
1234567.99 (obviously all thousands digits are optional)
In this way: 1.234.567,99
I know it is possible with Eval, but I didn't find an useful guide to do this.
Could you help me?
Thanks
There is an overload of Eval that takes three parameters (the link also contains a sample):
Container
Expression
Format
For the format, you'd specify "{0:c}" or whatever format you like. For a list of the Standard Numeric Format Strings, see this link. If you want to specify your format with a custom format string, e.g. use "{0:#,##0.00}".
You can use the ToString() extension
var value = 1234567.99;
Console.WriteLine(value.ToString("C3", CultureInfo.CurrentCulture));
Or by stating your culture
Console.WriteLine(value.ToString("C3", CultureInfo.CreateSpecificCulture("sv-SE")));

Regular Expression format for 0000/123456/23

I have a text box for Registration Number and i want it to use the following format 2013/123456/25. I want to validate for in-correct format using Regular Expression Validator.
The exact format must be first 4 numbers/6 numbers/2 numbers -->(2013/123456/25)
Thank you for your help...
You could use something like this :
^\d{4}/\d{6}/\d{2}$

No Way to Format Cell as Month-Date-Year in PHPExcel?

I've am trying to format cells in an Excel document I create with PHPExcel using the setFormatCode method. I've been going through the list of 'FORMAT_DATE_xxxxxxx' options associated with this method, but I've been unable to find one that lists the month, date, and year in that order. Did I miss something here?
You missed FORMAT_DATE_XLSX14 and FORMAT_DATE_XLSX22
You also missed the fact that these are simply predefined string values, and that you can use any valid Excel numberformat string value that you set yourself.
$objPHPExcel->getActiveSheet()->getStyle('C9')
->getNumberFormat()
->setFormatCode(
'mm-dd-yyyy' // my own personal preferred format that isn't predefined
);

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

Resources