How to represent comma's in string? [duplicate] - c#

This question already has answers here:
.NET String.Format() to add commas in thousands place for a number
(23 answers)
Closed 9 years ago.
I have a integer like this i.e 3356890. I'm converting it to string and showing on screen.
Now I want to display like this 3,356,890.
How to do?

You can use:
value = 1234567890;
Console.WriteLine(value.ToString("0,0", CultureInfo.InvariantCulture));
// Displays 1,234,567,890
However, for the purpose of internationalization and localization, it's probably best to allow the user's current culture settings to determine how to format the number.
Further Reading
Custom Numeric Format Strings

Take a look please here,
Number string format

string res = string.Format(CultureInfo.InvariantCulture, "{0:#,##0}", 3356890);

int val = 3356890;
string valString = val.ToString("#,##0")

Related

C# - wrong string to single conversion [duplicate]

This question already has answers here:
How do I parse a string with a decimal point to a double?
(19 answers)
Closed 6 years ago.
I am writing a program to get data from microcontroller to PC. The data is in float format. I tried to convert the string into float using Convert.ToSingle(string), but the conversion result is wrong:
"0.11" is converted to 11, sometimes 12.
"0.10" is converted to 10. etc
As you can see, it is losing the leading 0. , which is unexpected. How could this happen?
Your problem is culture specific. In some cultures float numbers are separated by a , and in some they are separated by a .
In your case
String a = "0,11";
Convert.ToSingle(a)
should result in your desired outcome of 0,11.
So you should explicitly specify a relevant culture that uses . as decimal separator. One possibility is the invariant culture which is based on the English language.
Try the following:
String a = "0.11";
Convert.ToSingle(a, CultureInfo.InvariantCulture)

Force two decimal places in C# [duplicate]

This question already has answers here:
Leave only two decimal places after the dot
(13 answers)
Closed 8 years ago.
I've this problem and can't find a solution.
This is super easy and i don't know why can't i find a solution.
Problem:
if a value returns for example "16.60", in c# i'll read "16.6", but i need 0 as well, because of paypal API, wich only accepts a value with no decimal numbers, or if it has to have decimal numbers the minimum and maximum must be 2.
So how can i make this?
i've tried this:
string value_f = "16,6";
decimal value_f_d = decimal.Parse(value_f);
value_f_d = (decimal)Math.Round(value_f_d, 2);
value_f = value_f_d.ToString("#.##");
value_f = value_f.Replace(',', '.');
i want this output: 16.60, but gives this: 16.6
string output = value_f_d.ToString("#.00", CultureInfo.InvariantCulture);
(using System.Globalization in your using declarations at the top)

format decimal number using ToString [duplicate]

This question already has answers here:
Using String Format to show decimal up to 2 places or simple integer
(18 answers)
Closed 9 years ago.
If I got decimal number like 14.50 and I want to be represented like decimal 10.2
0000000014.50
how can I do this?
Thank you
Use custom numeric format string:
var value = 14.50m;
string valueString = value.ToString("0000000000.00");
0 is a placeholder: Replaces the zero with the corresponding digit if one is present; otherwise, zero appears in the result string.
If you don't have an issue with the data type being converted to string then you could use Padding in c#.
Refer the link below :
http://msdn.microsoft.com/en-us/library/66f6d830(v=vs.100).aspx

Find a format string for a decimal without % sign [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How to format a number as percentage without the percentage sign?
I have a decimal:
decimal value = 0.0123m;
The format string "P" will return a string which value is multiple with 100 and add sign % at the end. Example:
string s = value.ToString("P"); // s = "1.23 %"
Please help me to find a format string without including % sign on returned string.
That mean: s will be "1.23" (without sign %)
Please help me. Thanks.
You can set the PercentSymbol property of the NumberFormatInfo to be something other than a % sign.
In your case you could just set it to be an empty string.
More info here
To do this you'll need to define a custom culture with its own NumberFormatInfo. The link to the answer provided in the comments in a great solution:
How to format a number as percentage without the percentage sign?

How to convert 26750 string to 26,750 format in c# [duplicate]

This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
.NET String.Format() to add commas in thousands place for a number
Hi Guys,
I have got 26750 in my string variable, something like below:
string str = "26750";
Now before showing on page, I want it to be converted into "26,750" format using c#. This value can increase as well as decrease also according to the result, so my format should work in both the cases.
Please suggest!
EDIT:
As I have written I have got string type value in my variable, I am trying with below code, but it is not working for me.
spnMiles.InnerHtml = String.Format("{0:n}", Miles);
It is not changing to the number format.
Please suggest!
This question should answers yours:
.NET String.Format() to add commas in thousands place for a number

Categories