Format int with negative number - c#

I have file that match this pattern:
Name-003
Name-002
Name-001
Name0000
Name0001
Name0002
And I need a way to format a string to have this pattern.
If I use
string.Format("Name{0:0000}", index)
It give me:
Name-0003
Name-0002
Name-0001
Name0000
Name0001
Name0002
I need a way to specify the number of digit including the negative sign character.
I want a solution with String.Format()

You could look at the documentation for numeric formats (specifically the ; character):
string.Format("Name{0:0000;-000}", index)
... or you could learn how to use an if statement to select between two format strings based on the value of index.

Related

C# IndexOf in special persian character

in persian/arabic character, some character used optional on top or bottom of other character like ِ َ ّ ُ.
in my example if i use this character, indexOf not found my word. consider that persian/arabic is rtl language.
for example:
منّم => م + ن + ّ + م
C#:
"منّم".IndexOf("من");
return -1
javascript:
var index= ' منّم '.indexOf('من');
console.log(index);
what happened in C#. anyone can explain this?
By passing in StringComparison.Ordinal as an argument to the overloaded String.IndexOf(), you could have also done the following:
"منّم".IndexOf("من", StringComparison.Ordinal); // returns 0
Specifying CompareOptions.Ordinal as an option should work, together with the IndexOf method of CompareInfo.
CompareInfo info = CultureInfo.CurrentCulture.CompareInfo;
string str = "منّم";
Console.WriteLine(info.IndexOf(str, "من", CompareOptions.Ordinal));
Output is 0.
DotNetFiddle if you want to try it yourself.
You should learn about the different methods that .Net uses to compare/match strings.
Best Practices for Using Strings in .NET
Some overloads with default parameters (those that search for a Char
in the string instance) perform an ordinal comparison, whereas others
(those that search for a string in the string instance) are
culture-sensitive. It is difficult to remember which method uses which
default value, and easy to confuse the overloads.
The section String Operations that Use the Invariant Culture gives a short explanation about combining characters.

How to convert alphanumeric value to int in c#

I have alphanumeric value = 106bd87f9386b63b. I want to convert this value into integer in c# and store the converted value into database. Is there any possibilities to convert it.
Did you try to use this:
the strLong is a declared variable.
and see this for your reference: https://learn.microsoft.com/en-us/dotnet/api/system.globalization.numberstyles?view=netframework-4.7.2
long.Parse(strLong, System.Globalization.NumberStyles.HexNumber);
You can do it with multiple methods choose one which is suitable to you
Method 1:
If you want to do it with Regex Here is Example helpful in Detail
C# Regular Expression to match letters, numbers and underscore
Here is also a useful and detailed link
Find and extract a number from a string
Other Methods:
You can also use Char.IsNumber('your character here')
here is Doc link of Microsoft
https://learn.microsoft.com/en-us/dotnet/api/system.char.isnumber?view=netframework-4.7.2
For the above method you have to split your string into characters and then check if it is character or not using Char.isNumber() Method .
Instead of all these you can also split this and make a function and match ASCII values and detect weather it is a number of not .
What ever method you use just detect number or not and concatinate it into a string and then at the end convert this string into a number using Convert class provided by Microsoft . Here is the detailed link of conversion
How can I convert String to Int?

System.Console.Write("{0}{1:N2} {2}" what's {1:N2} referring to?

i know 0 is for the first element in the array etc... but what's 1:N2?
The format to be applied to the data. In this case two decimal number.
http://msdn.microsoft.com/en-us/library/aa720653(v=vs.71).aspx
{1:N2} means that the second parameter is formatted as a number with thousand seperators and a precision of 2 digits.
The index "1" to the left of the colon specifies the second of the arg parameters (zero-based indexing). The string "N2" to the right of the colon specifies the format to use on that parameter. Specifically, N2 means group-separator numeric format with 2 decimal places; for details, see the documentation on standard format strings at http://msdn.microsoft.com/en-us/library/dwhawy9k.aspx
In general, the format specifier is of the form { index[,alignment][ : formatString] }; for details, see the documentation: http://msdn.microsoft.com/en-us/library/ttxecb1c.aspx
That is Numeric formatting the second element. Formatting in .Net can be done on different data types like number, dates, enums. you can also create custom formats. you can get started on formatting here
Formatting Types

Formatting strings into groups of 3 digits by periods

I know I can format strings using the String.Format() method. Is it possible to format like this?
Example:
string: 1568
formatted: 1.568
string: 168794521
formatted: 168.794.521
string: 987
formatted: 987
Sorry that I can't make myself more clear.
You can format a number that way, but not a string. For example, if you have an integer value, you can use:
int value = 168794521;
string formatted = value.ToString("N0");
With the proper culture, this will format as shown.
If you are using a string, you would need to convert it. You could also explicitly provide a culture to guarantee "." as a thousands separator:
int value = Int32.Parse("168794521");
string formatted = value.ToString("N0", new CultureInfo("de-DE"));
string someNumericValue = "168794521";
int number = int.Parse(someNumericValue); // error checking might be appropriate
value.ToString("0,0", CultureInfo.CreateSpecificCulture("el-GR"));
This will put points in for thousand specifiers.
It's possible that if you want this, your culture may already do this.
Yes, you can do that. SteveX has written a great blog post on string formatting. You could also look at this blog post, and the MSDN documentation.
You probably want to look at the bottom of this documentation in the "More Resources" section for more info about different types of standard format strings.
Here is the relavant part from the SteveX blog on formatting numbers:
Currency {0:c}
Decimal (Whole number) {0:d}
Scientific {0:e}
Fixed point {0:f}
General {0:g}
Number with commas for thousands {0:n}

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.

Categories

Resources