This question already has answers here:
Closed 12 years ago.
Possible Duplicate:
Is there an easy way in .NET to get “st”, “nd”, “rd” and “th” endings for numbers?
Is there anything already built into C# that formats a number as a ranking position?
By "ranking position" is mean one of ["st","nd,"rd","th"] eg : (1st, 2nd, 3rd, 4th).
I know it'd be easy to write an extension for this, I'm just wondering if there is already something in the language to cater.
Cheers
No, there is nothing built-in for this purpose. Not sure if its relevant, but consider cultural differences if you span languages/cultures.
I've been corrected. Please see the duplicate questions as described in the comments above.
Not out-of-the-box; however, as you mentioned, an extension is pretty easy.
public static string ConvertToRankingPosition(this int value)
{
var digit = value % 10;
return value != 11 && digit == 1 : value + "st" :
value != 12 && digit == 2 : value + "nd" :
value != 13 && digit == 3 : value + "rd" :
value + "th"
}
Related
This question already has answers here:
Comma and (decimal) point: Do they need different handling?
(5 answers)
Closed 9 months ago.
I have some decimal input from my DataGridView; let's say 4,3.
I want to compare if it is between 3,5 and 4,4.
How should I write the numbers 3,5 and 4,4 in my code?
With my current attempt, I get some errors like
; expected
} expected
Operator || cannot be applied
I tried with Cultureinfo but I do not know how exactly use it.
Here is my code so far:
var updateLabStar = db.PatientLab.Where(x => x.BloodUrinId == item.BloodUrinId).FirstOrDefault();
if (Convert.ToDecimal(Result) < 3,5 || Convert.ToDecimal(Result) > 4,4)
{
updateLabStar.Interval = updateLabStar.Result + " *";
db.SaveChanges();
}
How to write decimal literal.
var updateLabStar = db.PatientLab.Where(x => x.BloodUrinId == item.BloodUrinId).FirstOrDefault();
if (Convert.ToDecimal(Result) < 3.5m || Convert.ToDecimal(Result) > 4.4m)
{
updateLabStar.Interval = updateLabStar.Result + " *";
db.SaveChanges();
}
This question already has answers here:
How to ignore value after first decimal point first value if it is 0 else take it if greater than 0?
(2 answers)
Remove trailing zeros
(23 answers)
Closed 5 years ago.
I am trying to remove last zero of a decimal value search a lot for this but all answers in web and stack overflow removing all zeros. My aim to display some zeros in last.
Sample
50.00000000
50.01000000
50.01010000
50.01011000
50.01010100
expected
50.00
50.01
50.0101
50.01011
50.010101
I dont lnow how to do it in c#.
I was tried many things like .ToString("G29"); and other answers available but its all giving me like
50.00
50.01
50.01
50.01
50.01
Please Help..
Try this:
decimal num = 50.00m;
decimal num2 = 50.01010100m;
Console.WriteLine(num.ToString("0.00#########"));
Console.WriteLine(num2.ToString("0.00#########"));
Output:
50.00
50.010101
(Like suggested in comments by "Alex" and "Erno de Weerd")
You could use TrimEnd to remove the last zeros like this:
decimal num = 50.01011000m;
if (num % 1 == 0)
{
return num.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}
string str = num.ToString().TrimEnd('0');
Console.WriteLine(str);
output:
50,01011
You could also pack it into an extension method:
public static class Extensions
{
public static string ToTrimmendString(this decimal num)
{
if (num % 1 == 0)
{
return num.ToString("0.00", System.Globalization.CultureInfo.InvariantCulture);
}
return num.ToString().TrimEnd('0');
}
}
and call it like this:
Console.WriteLine(num.ToTrimmendString());
You can try
double num = 50.010110000;
Console.WriteLine(num.ToString("0.0000", CultureInfo.InvariantCulture) );
Please see this for more info https://learn.microsoft.com/en-us/dotnet/standard/base-types/custom-numeric-format-strings#SpecifierPt`
you can try something like this
public decimal removeTrailingZeros(decimal val)
{
return val % 1 == 0 ? Math.Round(val, 2) : val / 1.0000000000000000000000000000000000m;
}
test input
50.00000000m,
50.01000000m,
50.01010000m,
50.01011000m,
50.01010100m,
output
50.00
50.01
50.0101
50.01011
50.010101
This question already has answers here:
Always return positive value
(9 answers)
Closed 5 years ago.
I am calculating the difference between two numbers. If the calculation ends up being 5 - 10, it equals to "-5". If this is the case I need results to display/equal to "+5" , with the "+" sign.
I basically need reverse. So same if 10 - 5 quals to "5" I need it to display as "+5"
Code below I am using:
float rowresults = ROW1 - ROW2;
Textbox.text = rowresults.ToString();
Math.Abs is what you are looking for:
float rowresults = Math.Abs(ROW1 - ROW2);
And to add the "+"-sign to the front of the text (without changing your elsewise existing behaviour):
Textbox.text = "+" + rowresults.ToString();
This question already has answers here:
Using regular expressions to validate a numeric range
(11 answers)
Closed 2 years ago.
I would like to use a regex for a textbox that allows only numbers between 1-5000
I've tried the following but it wont work:
#"/^(?:1|5000|-[1-9]\d?)$/
You can use ^(?:[1-9]|\d{2,3}|[1-4]\d{3}|5000)$. But you'd better to parse to Int then do simple maths.
With some parsing beforehand, you can make the regex very simple:
string s = textBox1.Text;
string r = "";
int n = 0;
if (int.TryParse(s, out n) && (n>=1 && n<=5000))
{
r = "y";
}
if (Regex.IsMatch(r, "y")) {
// input was valid
MessageBox.Show("OK");
}
Try ...
^(?:[1-4][0-9]{1,3}|[1-9][0-9]{0,2}|5000)$
You can do something like the following:
^(([1-4][0-9]{0,3})|([1-9][0-9]{0,2})|(5000))$
The first two groups will match anything in the range of 1 - 4999. You add the |5000 at the end to make it match the range 1 - 5000. The three cases here are:
The number is exactly 5000
The number is between 1 and 3 digits. In this case, it can't possibly be more than 5000. However, the first number must be 1 - 9 so that you can't get something like "009" or "000."
The number is 4 digits, in which case it must be between 1000 - 4999
With that said, I think it would probably be simpler to just parse the int and see if it's in range.
You can try something like this (0-366)
^(0?[0-9]?[0-9]|[1-2][0-9][0-9]|3[0-5][0-9]|36[0-6])$
This question already has answers here:
Identify if a string is a number
(26 answers)
Regex for numbers only
(20 answers)
Closed 8 years ago.
How to check the string is number or not. I am verifying mobile number codes in which it should have 10 digits and only in numerical format.
string str="9848768447"
if(str.Length==10 && Here I need condition to check string is number or not)
{
//Code goes here
}
I am new to programming. please help me
Use int.TryParse:
int i;
if(str.Length==10 && int.TryParse(str, out i))
{
//Code goes here
}
Another way which has issues with unicode digits is using Char.IsDigit:
if(str.Length==10 && str.All(Char.IsDigit))
{
}