Sorry for reactivating this question, but I didn't find the right answer here.
In formatting numbers you can use 0 as a mandatory place and # as an optional place.
So:
// just two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
You can also combine 0 with #.
String.Format("{0:0.0#}", 123.4567) // "123.46"
String.Format("{0:0.0#}", 123.4) // "123.4"
String.Format("{0:0.0#}", 123.0) // "123.0"
For this formating method is always used CurrentCulture. For some Cultures . will be changed to ,.
Answer to original question:
The simpliest solution comes from @Andrew (here). So I personally would use something like this:
var number = 123.46;
number.ToString(number % 1 == 0 ? "0" : "0.00");
Answer from Gh61 on Stack OverflowSorry for reactivating this question, but I didn't find the right answer here.
In formatting numbers you can use 0 as a mandatory place and # as an optional place.
So:
// just two decimal places
String.Format("{0:0.##}", 123.4567); // "123.46"
String.Format("{0:0.##}", 123.4); // "123.4"
String.Format("{0:0.##}", 123.0); // "123"
You can also combine 0 with #.
String.Format("{0:0.0#}", 123.4567) // "123.46"
String.Format("{0:0.0#}", 123.4) // "123.4"
String.Format("{0:0.0#}", 123.0) // "123.0"
For this formating method is always used CurrentCulture. For some Cultures . will be changed to ,.
Answer to original question:
The simpliest solution comes from @Andrew (here). So I personally would use something like this:
var number = 123.46;
number.ToString(number % 1 == 0 ? "0" : "0.00");
An inelegant way would be:
var my = DoFormat(123.0);
With DoFormat being something like:
public static string DoFormat( double myNumber )
{
var s = string.Format("{0:0.00}", myNumber);
if ( s.EndsWith("00") )
{
return ((int)myNumber).ToString();
}
else
{
return s;
}
}
Not elegant but working for me in similar situations in some projects.
You can use the decimal.ToString override to specify a formatting.
decimal amount = 120.00m;
string str = amount.ToString("0.00");
This can also be used when using String.Format.
Console.WriteLine("{0:0.00}", amount);
In the case of your first rule, it cannot be done on one line.
decimal amount = 120.00m;
string str = amount.ToString("0.00").Replace(".00", String.Empty);
There are different overloads for decimal.ToString based on what formatting you want.
Example
decimal d = 5.00
Console.WriteLine(d.ToString("C")); // for currency
See below for other overloads... specifier is what you put into the ToString(specifier)
MSDN Documentation on Decimal.ToString
decimal value = 16325.62m; string specifier;
// Use standard numeric format specifiers.
specifier = "G";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: G: 16325.62
specifier = "C";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: C: $16,325.62
specifier = "E04";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: E04: 1.6326E+004
specifier = "F";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: F: 16325.62
specifier = "N";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: N: 16,325.62
specifier = "P";
Console.WriteLine("{0}: {1}", specifier, (value/10000).ToString(specifier));
// Displays: P: 163.26 %
// Use custom numeric format specifiers.
specifier = "0,0.000";
Console.WriteLine("{0}: {1}", specifier, value.ToString(specifier));
// Displays: 0,0.000: 16,325.620
specifier = "#,#.00#;(#,#.00#)";
Console.WriteLine("{0}: {1}", specifier, (value*-1).ToString(specifier));
// Displays: #,#.00#;(#,#.00#): (16,325.62)
The format method automatically does the commas and periods or visa versa (depending on whether your profile is in the US or elsewhere)
Assuming the $ sign applies to all cases in your org you can simply use
Decimal input = 2000;
String output = '$' + String.valueOf(input.format());
Note the String.valueOf(). This is required because the input variable is defined as a Decimal.
Edit: I noticed a bug with the Decimal.format() method where it will not show any numbers past the decimal point if there are only zeros there, ex. $100.00. To solve this I came up with this method.
private String getCents(Decimal x){
String y = String.valueOf(x);
String z = '.';
if(y.contains(',')) z = ',';
y = y.substring(0, y.indexOf(z));
if(x - Decimal.valueOf(y) == 0)
return String.valueOf(x.format()) + z + '00';
else return String.valueOf(x.format());
}
Then to update the example above, it would be:
Decimal input = 2000;
String output = '$' + getCents(input);
This should work, if its until 2 decimals:
Decimal dec;
String amount;
if (!string.valueof(dec.format()).right(3).contains('.')){
amount = '$' + string.valueof(dec.format()) + '.00';
}else if (string.valueof(dec.format()).right(2).contains('.')){
amount = '$' + string.valueof(dec.format()) + '0';
}else {
amount = '$' + string.valueof(dec.format());
}
Yes you can do it with String.format:
String result = String.format("%.2f", 10.0 / 3.0);
// result: "3.33"
result = String.format("%.3f", 2.5);
// result: "2.500"
You want java.text.DecimalFormat.
DecimalFormat df = new DecimalFormat("0.00##");
String result = df.format(34.4959);
Use the str() builtin, which:
Returns a string containing a nicely printable representation of an object.
E.g:
>>> import decimal
>>> dec = decimal.Decimal('10.0')
>>> str(dec)
'10.0'
Use the string format function:
>>> from decimal import Decimal
>>> d = Decimal("0.0000000000000123123")
>>> s = '{0:f}'.format(d)
>>> print(s)
0.0000000000000123123
If you just type cast the number to a string it won't work for exponents:
>>> str(d)
'1.23123E-14'