Format number with comma separator for thousands using C# [duplicate] - c#

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
String.Format an integer to use 1000's separator without decimal places or leading 0 for small integers
The blog post
http://blog.stevex.net/string-formatting-in-csharp/
(in the Custom number formatting section) shows that using the format {0:0,0}, a number like 1500 will be formatted as 1,500 which is good. But I don't understand why 0 is formatted as 00
Do I need to handle the case of 0 separately which doesn't seem to be necessary.

You can use "F0", which is "fixed number with 0 decimal places". It give you a thousands separator when you results go over 1,000.

Related

How to add zeroes Infront of an integer in c# [duplicate]

This question already has answers here:
Convert integer to binary in C#
(22 answers)
C# convert int to string with padding zeros?
(15 answers)
Closed 2 days ago.
I have a data I'm getting using my form and then I need to send it to a different software as an 8 digits number (in binary) the problem is the zeroes that come before the first '1' to appear in the data disappear.
for example:
the number 3 in binary is 00000011 so what will be send is 11
the letter A in binary is 01000001 so what will be send is 1000001
I need a code that will recognize how many digits in my data and add zeroes in front until the number would be 8 digits in c#
I looked online but all I could find is adding two known integers together but since I don't know how many digits would be missing from my data (as I don't know what the data would be yet, depends on the user), I can't use that.

Why C# decimal.ToString behaves differently when literal has .0 [duplicate]

This question already has answers here:
Why does a C# System.Decimal remember trailing zeros?
(3 answers)
Closed 2 months ago.
Executed in c# interactive
> 200M.ToString()
"200"
> 200.0M.ToString()
"200.0"
> 200.000000000000M.ToString()
"200.000000000000"
I was mind blown by this. Why this happens?
200 and 200.0 are the same numerically (200m == 200.0m returns true), of course, but "200" and "200.0" are certainly not the same string, and certainly will not pass a string.Equals() check
Decimal class always retains things like precision and scale. Decimal is intended for monetary calculations, so we definitely want it to exhibit this sort of meticulous behavior

How to format integer value as string with symbol "+" if its positive in C#? [duplicate]

This question already has answers here:
Custom numeric format string to always display the sign
(6 answers)
What is the percentage format string to always display the sign?
(4 answers)
Closed 5 months ago.
I want to print values as "+ 10 %" and "- 10 %".
For percentage I use myValue.ToString("P0") and its output is: 10 %, - 10 %.
How to add "+" symbol to that?
Thanks a lot
The options available are at https://learn.microsoft.com/en-us/dotnet/standard/base-types/standard-numeric-format-strings#PFormatString
There is a NegativeSign option to change the negative symbol, but not the positive one, so in that case you will have to fall back to having your own conditional code to use the P0 format, or multiply your value by 100 and use a conditional format specifier as described in the answer linked in the comments, Custom numeric format string to always display the sign .

How to create a display format for a number divided by 1000? [duplicate]

This question already has answers here:
String Format Numbers Thousands 123K, Millions 123M, Billions 123B
(3 answers)
Closed 5 years ago.
i have decimals and want to display them as "thousand €" but without any positions after the decimal point.
For instance 56000.45 should be displayed as 56 T €.
I have tried several ways and read about using the "," for division, and have tried adding it to this expression: {0:N0} T € but without success.
Could anyone point me in the right direction please?
Thanks in advance!
You could try
decimal currency = (56000.45 - (decimal) 56000.45 % 1000) /1000;
d will now equal 56.00, you can use
Math.Round(currency)
to get the value 56

How to convert string to decimal with specific number of digits [duplicate]

This question already has answers here:
Formatting a double to two decimal places
(8 answers)
Closed 6 years ago.
I have a small question regarding Math.Round function.
I need the string "12.123456" to be rounded at 4 decimals. I used:
Math.Round(Convert.ToDouble(pData), 4).ToString()
where pData is defined as string, but the values are decimal with 7 decimals.
My problem is that I expected to get every time the exact 4 decimals, but for some values it gives me only 2 (eg. 12.12 instead of 12.1200).
How can I change in order to always get the needed 4 decimals?
Regards,
You should use format strings instead:
pDate.ToString("0.0000")
or
pDate.ToString("n4")

Categories

Resources