String Formats not working in Telerik MVC Grid - c#

According to the following article, if you want to format the max number of decimal places to say 4, but have it chop off trailing zeros 0, you use something like this:
columns.Bound(p => p.Rate).Width(52).Format("{0:####.####}")
http://www.csharp-examples.net/string-format-double
But it doesn't remove the trailing zeros. I need to really save space in this grid, it has like over 10 rate columns that need to be displayed. In most cases the rates only go to 2 or 3 places, but a few of them need 4.
Steve

There is nothing wrond with your formating.
It should have been working for your case, unless you send your decimal values already formatted (i.e. converted into string)

Related

Change IXLCell number format from decimal point (.) to decimal comma (,) - c#

So it's pretty simple..(at least I thought so...)
I'm trying to export some data to excel from my app and I can't find a solution how to change decimal point to decimal comma (this is also an European standard)
This is the code currently and numbers are written like 12,561.00
var cell = worksheet.Cell(i, 1);
cell.SetDataType(XLDataType.Number);
cell.Style.NumberFormat.SetFormat("#,##0.00");
I can't find solution for changing this code to show it like 12.561,00
I've tried obvious solution like this and similiar variations:
cell.Style.NumberFormat.SetFormat("#.##0,00");
Also I've tried these solutions from this link
I suppose I have to change format number globally in whole worksheet or workbook but I couldn't find any option that could work.
Thanks!

custom string format puzzler

We have a requirement to display bank routing/account data that is masked with asterisks, except for the last 4 numbers. It seemed simple enough until I found this in unit testing:
string.Format("{0:****1234}",61101234)
is properly displayed as: "****1234"
but
string.Format("{0:****0052}",16000052)
is incorrectly displayed (due to the zeros??): "****1600005252""
If you use the following in C# it works correctly, but I am unable to use this because DevExpress automatically wraps it with "{0: ... }" when you set the displayformat without the curly brackets:
string.Format("****0052",16000052)
Can anyone think of a way to get this format to work properly inside curly brackets (with the full 8 digit number passed in)?
UPDATE: The string.format above is only a way of testing the problem I am trying to solve. It is not the finished code. I have to pass to DevExpress a string format inside braces in order for the routing number to be formatted correctly.
It's a shame that you haven't included the code which is building the format string. It's very odd to have the format string depend on the data in the way that it looks like you have.
I would not try to do this in a format string; instead, I'd write a method to convert the credit card number into an "obscured" string form, quite possibly just using Substring and string concatenation. For example:
public static string ObscureFirstFourCharacters(string input)
{
// TODO: Argument validation
return "****" + input.Substring(4);
}
(It's not clear what the data type of your credit card number is. If it's a numeric type and you need to convert it to a string first, you need to be careful to end up with a fixed-size string, left-padded with zeroes.)
I think you are looking for something like this:
string.Format("{0:****0000}", 16000052);
But I have not seen that with the * inline like that. Without knowing better I probably would have done:
string.Format("{0}{1}", "****", str.Substring(str.Length-4, 4);
Or even dropping the format call if I knew the length.
These approaches are worthwhile to look through: Mask out part first 12 characters of string with *?
As you are alluding to in the comments, this should also work:
string.Format("{0:****####}", 16000052);
The difference is using the 0's will display a zero if no digit is present, # will not. Should be moot in your situation.
If for some reason you want to print the literal zeros, use this:
string.Format("{0:****\0\052}", 16000052);
But note that this is not doing anything with your input at all.

Adding commas to thousands in C#

I'm trying to add commas to the following line of code:
Console.WriteLine(String.Format("{0, 8} {1,8} {2,8}", number, square,
cube));
How does one use alignment formatting in conjunction with adding commas?
It's this way
{0,8:N2}
N2 will format with comma based on locale.
Output sample could be useful... This: String.Format("{0, 8}, {1,8}, {2,8}", number, square, cube)); ?
Or you are looking for Number formatting that has thousands separator? Than you need to specify desired CultureInfo as first argument of the String.Format.
Try adding in the commas to the numbers before performing the alignment formatting (modifying based on your locale/culture, if necessary):
Console.WriteLine(
String.Format("{0, 8} {1,8} {2,8}",
number.ToString("#,0"),
square.ToString("#,0"),
cube.ToString("#,0")
)
);
And as Jeff points out in his comment below, you can also accomplish this by including the comma formats inline with the alignment formatting (the first part of each format block gives the alignment, the second part formats the string):
Console.WriteLine("{0,8:#,0} {1,8:#,0} {2,8:#,0}", number, square, cube);

Formatting currency in .NET

I am formatting the currency using Tostring() method i m using following syntax
ToString('##.##') it is working perfectly but in case of round number it remove last 2 zero
like for 100 it does not show 100.00 is shows 100.
how can i format in that way means
input desired output
100 100.00
100.10 100.10
Try "##.00" instead.
That will force two digits after the decimal separator.
You can also use ToString("C") to use the culture specific format in Windows directly.
First google result.
String.Format("{0:C}", x.ToString());
http://www.howtogeek.com/howto/programming/format-a-string-as-currency-in-c/
You can use :
.ToString("C")
Hope it helps.
Also, if you don't want the currency sign ($ in the US) added that "C" gives, you can also use "F2", which is "fixed number with 2 decimal places". It also has the advantage of giving you a thousands separator when you results go over 1,000.00.
This might help. Might be more than you need, but it takes globalization into account which might be necessary. "C" is also a short-cut currency format string that might get you further along.

Format string using multiple specifiers

Is there a way to use Int32.ToString("<some string format specifier>") with using more than 1 specifiers?
Specifically, I want to format an int in Hexadecimal but force the string to be 8-bit long, by adding 0's in the empty spots.
For example, I want to parse the number 1234 in decimal to the string "000004D2".
The way I wanted to do this was by combining the specifiers "X" and "00000000", but I can't seem to find any examples of combining specifiers together. Do I need to create my own FormatProvider?
I need to do this because I am writing a viewer which displays an array of bytes which supports different packages and formats. For example, display the array as an array of 4-bytes integers in hexadecimal, or 2-bytes integers in signed display. Much like the Memory viewer in VS
For that specific example, you can just use "X8" as your format specifier. I don't know about the more general case - but if you have any other specific requirements, it's probably worth asking about those separately.

Categories