How to validate decimal places - c#

Please let me know good way to validate decimal value, if decimal(4,2) it should accept 2 numeric and 2 decimal places.
var value = "44.29";
var dec = value.Split('.');
Then finding the length will can be used, I need a better culture specific way. I need a generic solution which can be applied to all decimal field.
Like:
validate(int before,int afterdecimal);
var valid = validate(2,2);
Need a generic cleaner solution for this
private static bool IsDecimal(string value, int before, int after)
{
if (value.Contains("."))
{
var parts = value.Split('.');
if (parts[0].Length == before && parts[1].Length == after)
return true;
}
else if(value.Length == before)
return false;
return true;
}

You can try like this:
[RegularExpression(#"^\d{1,2}(\.\d{0,2})$",ErrorMessage = "Value contains more than 2 decimal places")]
public decimal Value { get; set; }

If you whant just validate, try to use the mod:
44.29 % 1 = 0.29

From the above answers I was able to do it like this
string value = "2009.99";
if (IsDecimal(value, 4, 4))
{
Console.WriteLine("Valid");
}
private static bool IsDecimal(string value, int before, int after)
{
var r = new Regex(#"^\d{1," + before + #"}(\.\d{0," + after + #"})$");
return r.IsMatch(value);
}

Related

Get a variable value from a method to another?

In this situation, I am trying to get the value of "FileSizeType", an integer variable, into the method that's under it "NomCategorie"and convert it to a string(that's what the comment says).
static int ChoisirCategory()
{
int FileSizeType;
Console.Write("What type do you want: ");
FileSizeType = Convert.ToInt32(Console.ReadLine());
return FileSizeType;
}
static string NomCategorie(int c)
{
//get FileSizeType into c
string FileType;
if (c == 1)
{
return FileType = "Small";
}
else if (c == 2)
{
return FileType = "Medium";
}
else if (c == 3)
{
return FileType = "Large";
}
else if (c == 4)
{
return FileType = "Huge";
}
else
{
return FileType="Non-valid choice";
}
I would suggest using enum class
public enum Test
{
Small = 1,
Medium = 2,
Large = 3,
Huge = 4
}
then you can simply convert the number by using
int integer = 1;
if (Enum.IsDefined(typeof(Test), integer)
{
Console.WriteLine((Test)integer).
}
else
{
Console.WriteLine("Bad Integer");
}
output:
Small
Looking at your existing code, you are already returning the value of FileSizeType from the ChoisirCategory, so you can capture it in a variable and then pass that to the NomCategorie method to get the category name, for example:
int categoryId = ChoisirCategory();
string categoryName = NomCategorie(categoryId);
Note that there are many other improvements that can be made (for example, what happens if the user types in "two" instead of "2"?), but I think those suggestions may be out of scope based on the question.
Here's how your code could be simplified if you combine both suggestions above. I've also added an if clause to verify that the value is not higher than those available.
static enum AvailableSizes
{
Small = 1,
Medium = 2,
Large = 3,
Huge = 4
}
static int ChoisirCategory()
{
int FileSizeType;
GetInput:
Console.Write("What type do you want: ");
FileSizeType = Convert.ToInt32(Console.ReadLine());
// Ensure the value is not higher than expected
// (you could also check that it is not below the minimum value)
if (FileSizeType > Enum.GetValues(typeof(AvailableSizes)).Cast<int>().Max());
{
Console.WriteLine("Value too high.");
goto GetInput;
}
return FileSizeType;
}
static string NomCategorie(int c)
{
if (Enum.IsDefined(typeof(AvailableSizes), c)
{
return (AvailableSizes)c;
}
else
{
return "Invalid category";
}
}
Then somewhere in your code you would call these using this statement
string categoryStr = NomCategorie(ChoisirCategory());
Console.WriteLinte(categoryStr); // or do whatever you want with the returned value
With this code, if the input is higher than 4, it will output "Value too high." and ask the question again until the value is no higher than 4.
If the user types 0 or a negative value, then it will output "Invalid category".
This might help you decide where you want to handle input errors: right after user input or during the parsing of the number to a string.

Is there an easier way to parse an int to a generic Flags enum?

I've got a generic function to parse an object into a generic Enum.
However, I'm running into an issue when trying to safely parse an int into a [Flags] Enum.
Directly using Enum.ToObject() works to parse valid combinations, but will just return the original value if there isn't a flag combination.
Additionally, when there's no explicit enum member for a combination of flags, Enum.ToName() and Enum.IsDefined() don't return helpful values.
For Example:
[Flags]
public enum Color
{
None = 0,
Red = 1,
Green = 2,
Blue = 4,
}
// Returns 20
Enum.ToObject(typeof(Color), 20)
// Returns ""
Enum.ToName(typeof(Color), 3)
// Returns false
Enum.IsDefined(typeof(Color), 3)
I've written a function that I think technically works, but it seems like there has to be a better way to do this.
My Function:
public static T ParseEnumerator<T>(object parseVal, T defaultVal) where T : struct, IConvertible
{
Type ttype = typeof(T);
if (!ttype.IsEnum)
{
throw new ArgumentException("T must be an enumerated type");
}
bool isFlag = ttype.GetCustomAttribute(typeof(FlagsAttribute)) != null;
try
{
if (parseVal == null)
{
return defaultVal;
}
else if (parseVal is T)
{
return (T)parseVal;
}
else if (parseVal is string)
{
return (T)Enum.Parse(ttype, parseVal.ToString(), true);
}
//**************** The section at issue **********************************/
else if (isFlag && parseVal is int)
{
List<string> flagsList = new List<string>();
int maxVal = 0;
//Loop through each bit flag
foreach (var val in Enum.GetValues(ttype))
{
if (CountBits((int)val) == 1)
{
if ((int)val > maxVal)
maxVal = (int)val;
// If the current bit is set, add the flag to the result
if (((int)parseVal & (int)val) == (int)val)
{
string enumName = Enum.GetName(ttype, val);
if (!string.IsNullOrEmpty(enumName))
flagsList.Add(enumName);
}
}
}
// Is the value being parsed over the highest bitwise value?
if ((int)parseVal >= (maxVal << 1))
return defaultVal;
else
return (T)Enum.Parse(ttype, string.Join(",", flagsList));
}
//************************************************************************/
else
{
string val = Enum.GetName(ttype, parseVal);
if (!string.IsNullOrEmpty(val))
return (T)Enum.ToObject(ttype, parseVal);
else
return defaultVal;
}
}
catch
{
return defaultVal;
}
}
Is there something I'm missing? Or is there another way to parse these values safely?
Any help is appreciated, thanks!
-MM
Since your generic function has to know the Enum type to begin with, you can just scrap the function and use basic casting instead.
using System;
namespace SO_58455415_enum_parsing {
[Flags]
public enum CheeseCharacteristics {
Yellow = 1,
Mouldy = 2,
Soft = 4,
Holed = 8
}
public static class Program {
static void Main(string[] args) {
CheeseCharacteristics cc = (CheeseCharacteristics)12;
Console.WriteLine(cc);
}
}
}
If all you want to know is if you have a value that can be created using the enum flags.. that's pretty easy, as long as we can assume that each flag is "sequential" (e.g. there are no gaps between the flags). All numbers between 1 and the sum of all flag values can be made by some combination of flags. You can simply sum the flag values together and compare that to your question value.
public static bool IsValidFlags<T>(int checkValue) where T:Enum {
int maxFlagValue = ((int[])Enum.GetValues(typeof(T))).Sum();
return (0 < checkValue) && (checkValue <= maxFlagValue);
}
For future reference, you can constrain your generic parameter to an enum:
void fx<T> () where T:Enum { }

C# code to convert string to Double

I am trying to convert some particular types of strings into double in c#. Normally, Convert.ToDouble() works all great but this one does not always return healthy strings. Which means that input does not always come in format of "4.2". SOmetimes it also comes in form of 4.4.2. Now, i can also not rely on positioning and truncating since it can be 10.11.445 tomorrow?
Any easy and short string manipulation function that I can apply on this scenario?
struct CalifornicatedNumber
{
private string value;
public CalifornicatedNumber(string value)
{
this.value = value;
}
static public implicit operator CalifornicatedNumber(string value)
{
return new CalifornicatedNumber(value);
}
static public implicit operator CalifornicatedNumber(double value)
{
return new CalifornicatedNumber(value.ToString());
}
static public implicit operator double(CalifornicatedNumber calif)
{
return double.Parse(MakeItMakeSense(calif.value));
}
static private string MakeItMakeSense(string calif)
{
if (calif.Count(x => x == '.') > 1)
calif = calif.Substring(0, calif.IndexOf('.', calif.IndexOf('.') + 1));
return calif;
}
}
then...
CalifornicatedNumber califnum;
califnum = "10.11.145";
Console.WriteLine(califnum);
if (califnum > 10) { Console.WriteLine("huzzah");}
califnum = 13.42;
Console.WriteLine(califnum);
if (califnum > 10) { Console.WriteLine("huzzahZah"); }
...this is the most foolish piece of code I have ever written.
After the posted comments I am assuming you would like to take the string 4.4.2 and convert it to double dropping everything after the second . (if found).
A method such as.
public static double ConvertStringToDouble(string inputString)
{
if (inputString.Count(x => x == '.') > 1)
inputString = inputString.Substring(0, inputString.IndexOf('.', inputString.IndexOf('.') + 1));
return double.Parse(inputString);
}
I would do an approach of creating a bunch of strategies for parsing the input text and then iterating through the strategies until a result is found.
First I'd define a tryParseDouble function:
Func<string, double?> tryParseDouble = t =>
{
double value;
if (double.TryParse(t, out value))
{
return value;
}
return null;
};
Then I'd create my list of strategies:
var strategies = new Func<string, double?>[]
{
t =>
{
var parts = t.Split('.');
return parts.Length > 1
? tryParseDouble(String.Join(".", parts.Take(2)))
: null;
},
tryParseDouble,
t => null,
};
And finally I'd get the result:
var text = "4.4.2";
var result =
strategies
.Select(p => p(text))
.Where(v => v != null)
.FirstOrDefault();
The result variable is a double? with the value parsed or a null if none of the strategies work. The final strategy, t => null, is there to be explicit but is not necessary in getting the final null result.
As new strategies are needed to parse different types of input text they can just be added to the list as needed.
I think this will do what you want according to your comments by parsing either the whole string (if no decimal points) or just the first two parts of the sting if there are multiple decimals.
String[] parts = stringVal.Split('.');
double doubleVal;
if (parts.length > 1)
{
doubleVal = Convert.ToDouble(parts[0] + "." + parts[1]);
}
else
{
doubleVale = Convert.ToDouble(stingVal);
}

How to cast string to int

I have a set of objects named something like this:
_1,_2,_3 .... _n
Isn't an array, just a bunch of objects uinto a custom control. I want to cast the name to an int. first I remove _, but If use
private void Superficie_MouseDown(object sender, MouseButtonEventArgs e)
{
int index = 0;
Shape myShape = ((Shape)sender);
string lcNombre = myShape.Name.Remove(0, 1);
//--------------------------------------------------------------------
// Those are my tryes
// Doesn't work because it must expect a nullable value
index = lcNombre as decimal;
// Doesn't work Can't convert string to int
index = (int)lcNombre;
//--------------------------------------------------------------------
if (index > 0)
{
bool lIsSelected = !Pieza.Superficies.Where(x=>x.Id == index).First().IsSelected;
Pieza.Superficies.Where(x => x.Id == index).First().IsSelected = lIsSelected;
if (lIsSelected)
myShape.Fill = new SolidColorBrush(SystemColors.HotTrackColor);
else
myShape.Fill = new SolidColorBrush(SystemColors.ControlLightLightColor);
}
}
How can I do to cast it ?
The most straightforward way is
index = int.Parse(lcNombre);
If you're not completely sure whether lcNombre is really an integer, you can use instead
int result;
bool isInt = int.TryParse(lcNobre, out result);
or in newer versions of .NET, you can declare the output variable like:
bool isInt = int.TryParse(lcNobre, out var result);
Finally you can use
index = Convert.ToInt32(lcNobre);
This approach (like int.Parse()) will throw a FormatException if the input value is not convertible to an integer.
You can also use the Convert static methods:
index = Convert.ToInt32(lcNombre);
MSDN Link: http://msdn.microsoft.com/en-us/library/sf1aw27b(v=vs.110).aspx
Error wise.. you may want to use TryParse.. as it returns true or false depending on a successful conversion:
int index;
if (int.TryParse(lcNombre, out index)) {
// conversion successful
}
else {
// couldn't convert
}
MSDN Link for int.TryParse: http://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx
try this
int result=0;
Int32.TryParse(lcNombre,out result);
My favorite way to do this is to make an extension method as follows
public static class ExtensionMethods
{
public static int? AsInteger( this string str)
{
int value;
if ( int.TryParse( str, out value ) )
{
return value;
}
else
{
return null;
}
}
}
then you can this
index = lcNombre.AsInteger() ?? 0;
string strArr[] = myShape.Name.Split('_',StringSplitOptions.RemoveEmptyEntries)
int num;
int.TryParse(strArr[0],num);

Need to return the value with an Currency sign

I have a small tab on an app that allows user to lookup a price in a price table and it returns the price. how can i get the '$' sign on the returned price?
public double? SearchMedicinePrice(Int64 pNDC)
{
double? retValue = null;
objConext = new FDBEntities();
Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault();
if (objMedicine != null)
{
retValue = objMedicine.Price;
}
return retValue;
}
When you call ToString() on a double (and many other types), you can pass in a format string to specify how the result should be formatted. Like this:
double price = 10.5;
price.ToString("C");
Output = $10.50
You could also use String.Format like this:
string message = String.Format("The price is {0:C}", SearchMedicinePrice(pndc));
{0:C} is a format string. The C specifies that the value should be formatted as currency.
You can also pass in a CultureInfo object to configure things like the currency symbol. For example, the following code will output £10.50
10.5.ToString("C", new CultureInfo("en-GB"))
You might also consider using decimal instead of double for storing currency data.
You could build a small object:
public class Currency {
public double Amount {get;set;}
public string Symbol {get;set;}
public override string ToString() {
return String.Format(Symbol + "{0}",Amount);
}
}
public Currency SearchMedicinePrice(Int64 pNDC) {
Currency retValue = null;
objConext = new FDBEntities();
Medicine objMedicine = objConext.Medicines.Where(med => med.PriceType == 9 && med.NDC == pNDC).OrderByDescending(item=>item.MedicineID).FirstOrDefault();
if (objMedicine != null)
{
retValue.Amount = objMedicine.Price;
retValue.Symbol = objMedicine.CurrencySymbol;
}
return retValue;
}
Then in to use it:
Label1.Text = SearchMedicinePrice(pNDC).ToString();
You have to use string
string str="$"+SearchMedicinePrice(x).ToString();

Categories