How I could remove leading zeros in string C# WPF - c#

After I convert a decimal value salePr to string, using following code:
decimal salePr;
string salePrStr;
...
salePrStr = (salePr).ToString("0000.00");
'''
I'd like to get rid of leading zeros (in case result is <1000).
What is right and the best way to do this operation?

So why have you explicitly included them? Just use a format string of 0.00.

You could use trimstart to remove the leading zeros.
salePrStr = (salePr).ToString("0000.00").TrimStart(Convert.ToChar("0"));

It looks like you are trying to display currency, if you want to display it as currency, try salePrStr = String.Format("{0:C}", salePr) otherwise use the format 0.00

salePrStr = (salePr).ToString("###0.00");

The other answers are probably what you're looking for. If, for some reason, however, you actually want to keep the original strings (with leading zeroes), you can then write:
string salePrStr = salePr.ToString("0000.00");
string salePrStrShort = salePrStr.TrimStart('0');

Give this a try:
salePrStr = (salePr).ToString("N2");
That would make 1000.10 show as
1,000.10
and make 45.2305 show as
45.23
Just testing it in c#

Related

Prevent C# from using 'E+x' with large numbers [duplicate]

How to convert double to string without the power to 10 representation (E-05)
double value = 0.000099999999833333343;
string text = value.ToString();
Console.WriteLine(text); // 9,99999998333333E-05
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
I've tried the following variants
Console.WriteLine(value.ToString()); // 9,99999998333333E-05
Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05
Console.WriteLine(value.ToString("N20")); // 0,00009999999983333330
Console.WriteLine(String.Format("{0:F20}", value)); // 0,00009999999983333330
Doing tostring N20 or format F20 seems closest to what I want, but I do end up with a lot of trailing zeros, is there a clever way to avoid this? I'd like to get as close to the double representation as possible 0.000099999999833333343
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
How about
Convert.ToDecimal(doubleValue).ToString()
You don't need string.Format(). Just put the right format string in the existing .ToString() method. Something like "N" should do.
Use string.Format with an appropriate format specifier.
This blog post has a lot of examples: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx

How can I trim currency symbol from string in C#? [duplicate]

I am trying to parse or convert a string to decimal in C#.
I need to be able to parse strings like,
$123,345,676.8999 to its equivalent 123345676.90.
I need to have only 2 places after the decimal point and that needs to be suitably rounded.
Can you guys please suggest a way to do the above? Basically, any string in the form of currency (with $,pound symbol etc). I should be able to parse it and convert it to decimal.
Try this:
var val = double.Parse("$123,345,676.8999", NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint | NumberStyles.AllowCurrencySymbol);
val = Math.Round(val, 2);
You can use decimal.TryParse to perform the parsing -- the link has some samples on how the parsing works.
Very simple answer: use Decimal.Parse()

How to insert a thousand separator (comma) with convert to double

I am trying to format the contents of a text box:
this.lblSearchResults1.Text =
Convert.ToDouble(lblSearchResults1.Text).ToString();
How do I amend this so that I the text includes comma/thousand separators?
i.e. 1,000 instead of 1000.
Looking at the standard numeric format strings:
You can most easily use 'N' which will do the right thing based on the user culture, so in your case you can just add "N" as a param to the ToString
([double]12345.67).ToString("N")
12,345.67
For complete custom control, use ... .ToString("#,##0.00") or variations thereof. The . and , will be replaced by culture dependent symbols. In most of europe you'd get 1.234,56.
Another useful picture is 0.0#.
To use a pattern depending on the users (or on a selected) culture, use The Numeric ("N") Format Specifier, as in .ToString("N") or "... {0:N}".
The easiest way to do it would be something like:
Convert.ToDouble("1234567.12345").ToString("N")
If you want to control the decimal places you can do something like:
Convert.ToDouble("1234567.12345").ToString("N3")
In general look at the overloads on ToString for more exciting possibilities.
If you take a closer look at Standard Numeric Format Strings you can try one of the following:
.ToString("n", CultureInfo.GetCultureInfo("en-US"))
.ToString("n", CultureInfo.GetCultureInfo("de-DE"))
.ToString("n", CultureInfo.CurrentCulture)
An alternative to the above mentioned responses would be to use
this.lblSearchResults1.Text = String.Format("{0:N}", Convert.ToDouble(lblSearchResults1.Text))
If you wanted decimal places, just enter the amount of decimal places you wish to have after the N. The following example will return the value with 2 decimal places.
this.lblSearchResults1.Text = String.Format("{0:N2}", Convert.ToDouble(lblSearchResults1.Text))
See http://msdn.microsoft.com/en-us/library/system.string.format.aspx for more information.
double.Parse(Amount).ToString("N");
Do not cast integral to double to do this!
Use NumberFormatInfo helper class, e.g:
var nfi = new NumberFormatInfo() {
NumberDecimalDigits = 0,
NumberGroupSeparator = "."
};
var i = 1234567890;
var s = i.ToString("N", nfi); // "1.234.567.890"

Custom Numeric Format Strings: Dynamic Decimal Point

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.

How to convert double to string without the power to 10 representation (E-05)

How to convert double to string without the power to 10 representation (E-05)
double value = 0.000099999999833333343;
string text = value.ToString();
Console.WriteLine(text); // 9,99999998333333E-05
I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:)
I've tried the following variants
Console.WriteLine(value.ToString()); // 9,99999998333333E-05
Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05
Console.WriteLine(value.ToString("N20")); // 0,00009999999983333330
Console.WriteLine(String.Format("{0:F20}", value)); // 0,00009999999983333330
Doing tostring N20 or format F20 seems closest to what I want, but I do end up with a lot of trailing zeros, is there a clever way to avoid this? I'd like to get as close to the double representation as possible 0.000099999999833333343
Use String.Format() with the format specifier. I think you want {0:F20} or so.
string formatted = String.Format("{0:F20}", value);
How about
Convert.ToDecimal(doubleValue).ToString()
You don't need string.Format(). Just put the right format string in the existing .ToString() method. Something like "N" should do.
Use string.Format with an appropriate format specifier.
This blog post has a lot of examples: http://blogs.msdn.com/kathykam/archive/2006/03/29/564426.aspx

Categories

Resources