Number Formatting String - c#

I want to make round a number to million. This is my code:
string formating = "#,#,,";
decimal abc = 1234567890m;
decimal abc2 = 0m;
string text = abc.ToString(formating); // text="1,235"
string text2 = abc2.ToString(formating); // text2=""
How to correct formatting so that text2="0"?
P/S: I use C# .Net 2.0.

You could use #,0,,.
Note that the number will be rounded, similar behaviour to your original format string:
Console.WriteLine(0m.ToString("#,0,,")); // 0
Console.WriteLine(499999m.ToString("#,0,,")); // 0
Console.WriteLine(500000m.ToString("#,0,,")); // 1
Console.WriteLine(1234567890m.ToString("#,0,,")); // 1,235

Try string formating = "#,##0"
Then you can write:
string text = (abc/1000000).ToString(formating); // text="1,235"
string text2 = (abc2/1000000).ToString(formating); // text2="0"

Related

Extra zeroes when parsing string to decimal

I want to parse a string entered in a WinForms text box as a decimal. These values are then stored in a property in an object. They should parsed to two decimal places, exactly like the following examples:
string amount1 = 2; // should be parsed as 2.00
string amount2 = 2.00; // should be parsed as 2.00
string amount3 = 2.5; // should be parsed as 2.50
string amount4 = 2.50; // should be parsed as 2.50
string amount5 = 2.509; // should be parsed as 2.51
How to do this? At the moment, I am parsing as follows:
decimal decimalValue = Decimal.Parse(stringValue);
There are two operations, parsing and rounding.
decimal decimalValue = Math.Round(Decimal.Parse(stringValue), 2);

6 Floating digit without separator in c#

I am trying to get 11001 as 11001.000000 , I tried ToString("N6"), but it adds separator and output is as: 11,001.000000
How can get the value as a 6 floating digit without separator?
Use F6 instead of N6
int i = 11001;
string result = i.ToString("F6");
Reference: MSDN
To remove the comma separaytor use can use
System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator
double d = 11001;
string result = d.ToString("F6").Replace(System.Threading.Thread.CurrentThread.CurrentCulture.NumberFormat.NumberGroupSeparator, "");
int i = 11001;
string result = i.ToString("F6");
Here F in ToString() method is fixed point format specifier.
Number next to F can be used accordingly.
ex if you want 11001.00000, then use ToString("F5");

show string in a desire format

I want to show my string in a special format.
For example I have a string like this s = "00012345"; and want to show it in this format 000(123-45)
I use this code for it:
label1.text = string.format("{0,###(###-##)}",s);
But just see this for result: 00012345.
What string formatting should I be using?
As mentioned you can't format an already existing string. First you need to convert the string to an int:
int num = Int32.TryParse(s);
Then you can do the formatting whilst converting it back into a string:
label1.text = num.ToString("000(000-00)");
string s = "00012345";
string s1 = s.Substring(0,3);
string s2 = s.Substring(3,3);
string s3 = s.Substring(6);
s = string.Format("{0}({1}-{2})", s1,s2,s3);

How can I convert formatted string to int (if I know format) in C#?

I have formatted string, for example 123 456 000$ and I know format of this string {0:### ### ##0}$. I'd like to get int value = 123456000; from this string in C# with using known format.
How I can do that?
mytext = System.Text.RegularExpressions.Regex.Replace(mytext.Name, "[^.0-9]", "");
or something approximately like that get's rid of the pesky non-numbers
and then delete the first few characters 1,2,3... and last few length, length -1, length -3...
unless I'm missing something?
Oh yeah, and Convert.Toint32(mytext)
int.Parse("123 456 000$", NumberStyles.AllowCurrencySymbol |
NumberStyles.Number);
http://msdn.microsoft.com/en-us/library/c09yxbyt.aspx
I think you will have to construct a similar NumberFormatInfo to parse the string value.$ for the currency symbol used in the text and in your case the thousand group seperator is space instead of , so a custom number format info should help you parse it.
string val = "123 456 000$";
NumberFormatInfo numinf = new NumberFormatInfo();
numinf.CurrencySymbol = "$";
numinf.CurrencyGroupSeparator = " ";
numinf.NumberGroupSeparator = " "; // you have space instead of comma as the seperator
int requiredval = int.Parse(val,NumberStyles.AllowCurrencySymbol | NumberStyles.AllowThousands, numinf);
This should help you get the value
I would make it easy way:
String formattedString = "12345500$";
formattedString.Remove(formattedString.Length - 1);
int value = int.Parse(formattedString);

using numbers in a string

Can I use numbers while using String data type?
Sure you can, and if you want to use them as numbers you can parse the string. E.g. for an integer:
string numberAsString = "42";
int numberFromString;
if (int.TryParse(numberAsString, out numberFromString))
{
// number successfully parsed from string
}
TryParse will return a bool telling if the parsing were successful. You can also parse directly if you know the string contains a number - using Parse. This will throw if the string can't be parsed.
int number = int.Parse("42");
You can have numbers in a string.
string s = "123";
..but + will concatenate strings:
string s = "123";
string other = "4";
Debug.Assert(s + other != "127");
Debug.Assert(s + other == "1234");
Numbers can be easily represented in a string:
string str = "10";
string str = "01";
string str = 9.ToString();
However, these are strings and cannot be used as numbers directly, you can't use arithmetic operations on them and expect it to work:
"10" + "10"; // Becomes "1010"
"10" / "10"; // Will not compile
You can easily store numbers as a string:
string foo = "123";
but that only helps you if you actually want numbers in a string. For arithmetic purposes, use a number. If you need to display that later, us a format string.
String number1 = "123456";
keep in mind. using that number for arithmatic purpose, you have to convert that string into proper type like
int number1Converted = Int32.Parse(number1);
int.TryParse(number1 , out number1Converted );
for double
double doubleResult = 0.0;
double.TryParse("123.00", out doubleResult);

Categories