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
Related
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")));
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
);
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
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
Is there any way to parse a string in the format HH:MM into a Date (or other) object using the standard libraries?
I know that I can parse something like "9/17/2008 10:30" into a Date object using
var date:Date = new Date(Date.parse("9/17/2008 10:30");
But I want to parse just 10:30 by itself. The following code will not work.
var date:Date = new Date(Date.parse("10:30");
I know I can use a custom RegEx to do this fairly easily, but it seems like this should be possible using the existing Flex API.
If you have to use the exact format you specified, then you need to parse it yourself.
Here is a simple example (not tested):
var str:String = "9/17/2008 10:30"
var items:Array = str.split(" ");
var dateElements:Array = items[0].split("/");
var timeElements:Array = items[1].split(":");
var n:Date = new Date(dateElements[2],
dateElements[0],
dateElements[1].
timeElements[0],
timeElements[1]);
If the time is not expressed in 24 clock, then there is no way to check for AM or PM (code will assume AM).
As a simple and free solution, you could use some static methods of the DateField:
DateField.stringToDate(valueString:String, inputFormat:String):Date
DateField.dateToString(value:Date, outputFattern:String):String
But unfortunately they don't support hours/minutes/seconds (just the date).
In your specific case: the Date object always contains also a "date" information.. if it isn't important, couldn't you simply concatenate a standard date string before parsing?
Have you considered prepending "01/01/2000 " to the time string and then applying Date?
Alternately there's probably a tokenizer that will take the input and split it up at the : giving you an array of strings you can convert to integers. A tokenizer isn't hard to write, either, and can be fun if one doesn't exist in flex.
-Adam
To answer your specific question: no, there's no library function to do what you want to do, but then there's no library function for parsing dates on the ISO format, on the German format, on the Swedish format, dates where the years are in roman numerals etc.
Why not use regular expressions? That's what they are for.