I want to get the currency value in following format after doing string.format.
I have used several formats but none of them are like what i wanted.
Value After Format
0 $0
123 $123
1234 $1,234
12345 $12,345
I have used following code and it works fine.
Console.WriteLine(string.Format("{00:$#,#}", 12345));
But the problem is that when value becomes 0 then it prints just $ whereas i want to print $0
Thanks
The # in the format string means display a digit if it is not zero. So when you have a 0 value, then it will not display anything.
So you need to change this to
Console.WriteLine(string.Format("{00:$#,0}", mynumber));
But you can also use the inbuilt currency string formatting option.
The docs have a whole section on currency formatting
https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#CFormatString
So then you can have
Console.WriteLine(string.Format("{0:c}", 99.556551));
Or if you don't want any decimals places then
Console.WriteLine(string.Format("{0:C0}", 99.556551));
Although this will round the value as well, so it might be better if you control this yourself.
And as reminded by #RufusL below, the string.format is not needed in a Console.Writeline, so you can also simplify this further;
Console.WriteLine("{0:C0}", 99.556551);
Related
I have the following code that formats a column as decimal
It works fine however if the number is less than 1 the zero is not displayed
foreach (var deciCol in decimalIndx)
{
var col = deciCol.Start.Column;
sheet.Column(col).Style.Numberformat.Format = "#.####";
}
Input 12.35486 ==> in excel 12.3548 (OK)
Input 0.34845 ==> in excel .3484 (0 is not displayed)
Input 0 ==> in excel (0.) (how can i remove the decimal separator)?
Thank you in advance
Edit:
Thanks to the answer below, i used the following:
"0.0###"
# means optional digit. Use 0 for a leading zero, eg "0.####".
The format string is the same format string you'd use in Excel if you selected a Custom format code. You can test the format string in Excel first and once you find the one you want, use it in EPPlus.
The contents of a custom numeric format string are documented in Excel's docs. Check Create or delete a custom number format. This explains how to specify different formats for positive, negative, zero amounts, include extra text etc.
It would seem that you can even specify colors in the format string. I wonder how [Blue]0.###;[Red]-0.### would look
UDPATE
As the linked page shows, you can specify a different format for zero, eg :
"0.####;-0.###;0"
I'm implementing string format number with three sections as
DataFormatString="{}{0:$#,##0.00;($#,##0.00);''}"
i.e:
input 120 result $120.00
input -120 result ($120.00)
input 0 result ''
the problem is the dollar sign is hard-coded and I want my app to globalization. I tried some thing like this:
DataFormatString="{}{0:C;C;''}"
but it doesn't help.
Perhaps try taking this approach:
var DataFormatString="$#,##0.00;($#,##0.00);''";
var amount = 1342.56m;
var formatted =
amount
.ToString(DataFormatString)
.Replace("$", CultureInfo.CurrentCulture.NumberFormat.CurrencySymbol);
The only downside is that it won't correctly place the currency symbol at the end of the number if that's the standard for that currency.
You can use the overload of string.Format with IFormatProvider like so:
string DataFormatString="{0:C}";
string output = string.Format(CultureInfo.CreateSpecificCulture("culture"),DataFormatString,input)
Demo
However, as Soner Gonul observes, this still won't generate '' as output for 0.
As far as I know, there is no direct format to supply that 3 results you want for those inputs.
For first two format, you can use The "C" format specifier with en-US culture (which has $ as a CurrencySymbol) using 2 as a precision specifier like;
(120).ToString("C2", CultureInfo.GetCultureInfo("en-US")) // $120.00
(-120).ToString("C2", CultureInfo.GetCultureInfo("en-US")) // ($120.00)
But for 0, this generates $0.00 which is not what you want.
As a better solution, check Enigmativity's answer instead which handles 0 value as well.
We have an import service for stores into our database. The latitude and longitude columns are not currently being formatted on import and have resulted in invalid values being inserted into our db. I therefore need to format any lat/long to have 3 places before the decimal and up to 6 after which I thought would be Format("###.######") but it doesn't seem to work. Input values such as 38.921322 or -12.235 have not seemed to conform to the formatting provided. Could anyone a bit more experienced in the area of C# string formatting shed light on how to achieve this? Thank you in advance.
Have you tried String.Format("{0:000.000000}", value);?
Console.WriteLine("{0:000.000000}", 123);
Outputs: 123.000000
Use "0" instead of "#". From MSDN:
0 Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
# Replaces the "#" symbol with the corresponding digit if one is present; otherwise, no digit appears in the result string.
i know 0 is for the first element in the array etc... but what's 1:N2?
The format to be applied to the data. In this case two decimal number.
http://msdn.microsoft.com/en-us/library/aa720653(v=vs.71).aspx
{1:N2} means that the second parameter is formatted as a number with thousand seperators and a precision of 2 digits.
The index "1" to the left of the colon specifies the second of the arg parameters (zero-based indexing). The string "N2" to the right of the colon specifies the format to use on that parameter. Specifically, N2 means group-separator numeric format with 2 decimal places; for details, see the documentation on standard format strings at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
In general, the format specifier is of the form { index[,alignment][ : formatString] }; for details, see the documentation: http://msdn.microsoft.com/en-us/library/ttxecb1c.aspx
That is Numeric formatting the second element. Formatting in .Net can be done on different data types like number, dates, enums. you can also create custom formats. you can get started on formatting here
Formatting Types
I am trying to format a double in C# such that it uses the thousand separator, and adds digits upto 4 decimal places.
This is straight forward except that I dont want to have the decimal point if it is an integer. Is there a way to do this using the custom numeric format strings rather than an if statement of tenary operator?
Currently I have:
string output = dbl.ToString(dbl == (int)dbl ? "#,##0" : "#,##0.####");
Thanks
I believe your second format string of "#,##0.##" should be exactly what you want -- the # format character is a placeholder that will NOT display zeros.
If you had "#,###.00" then you would get trailing zeros.
test code:
double d = 45.00;
Console.Writeline(d.ToString("#,##0.##"));
Gives output of "45". Setting d to 45.45 gives output "45.45", which sounds like what you're after.
So you had the answer after all! ;)
Incidentally, there's a handy cheat-sheet for format strings (amongst other handy cheat-sheets) at http://john-sheehan.com/blog/net-cheat-sheets/
No, there is not any built-in format string for this. Your current solution is the best way to accomplish this.
MSDN lists both the standard numeric format strings and custom numeric format strings, so you should be able to see for yourself that none directly matches your needs.