-2

As part of my work,I need to read price values from Excel sheet . I need to implement

  • Prices: Non-numeric characters in Price not allowed
  • price should be valid number for price like int,decimal,double etc like 10,10.00,10,233 valid, -10,-10.00,-10,2333.00 etc are invalid
  • Prices: Price format (dots, comma's, decimals)
  • Zero and negative price values are not allowed
  • Need to check price value type (number type like int,float,decimal etc but will save in database in money format)

What datatype I should we choose for Price ? decimal or double or anyother ? In database I took database field type as money.

Rakesh
  • 313
  • 1
  • 3
  • 14
  • `Price format (dots, comma's, decimals)` what's that supposed to mean? Some cultures use comma to separate thousands, others use it as a decimal separator. Which do *you* mean? – Matt Burland Apr 02 '15 at 14:35
  • 1
    `decimal` is *designed* to be used for money values - don't use IEEE-754 floating point values, as they do not represent an *exact* value: http://en.wikipedia.org/wiki/IEEE_754 – Binkan Salaryman Apr 02 '15 at 14:36
  • 2
    So, what's your question? The one in the title (*"I need a regular expression for..."*) or the one in the text (*"Which datatype should I choose for Price?"*)? – Heinzi Apr 02 '15 at 14:36
  • You must provide more examples, sample input, required output... Otherwise, it is difficult to help you and others having a similar problem. – Wiktor Stribiżew Apr 02 '15 at 15:06
  • Price format (dots, comma's, decimals): Yes you are right some culture are using different format so they can put prices value in that format. My question is "is these validation can be done by simply regular expression or or is there any other way". Client can put any kind of value for prices in any format (by mistake).So I need to validate those prices. – Rakesh Apr 02 '15 at 15:52

1 Answers1

0

I do not think you need any regex if you just need to validate price numbers in C#.

I'd suggest using Decimal type, here you can find why. The Decimal class contains a static TryParse method that can be used to validate numbers as valid decimal numbers. Here is a slightly modified example from MSDN (I decided to go with InvariantCulture, but it depends on whether your DB contains currencies in EN-US format or not):

var validated = false;
decimal number;
// Parse currency value using current culture. 
var value = "1,097.63";
var style = System.Globalization.NumberStyles.Number;
var culture = System.Globalization.CultureInfo.InvariantCulture;
if (!Decimal.TryParse(value, style, culture, out number))
    if (number > 0)    // Check if the value is not negative or zero
        validated = true;
Community
  • 1
  • 1
Wiktor Stribiżew
  • 607,720
  • 39
  • 448
  • 563