0

I need to change the format of a Decimal number depending on other values.

My customer is a performance venue and wants to charge usage of different expendable items in different formats, by example:

Replacement light globes - whole numbers only = Qty can only equal 1 or 2 or 3...
Man hours - charged in quarters - Qty could equal 1.25 (2 decimal points)
Electricity used - charged to 4 decimal points = Qty could equal 23.1234

So I want to be able to change to number format of Qty according to the type of item being charged:

if item = Light Globes, format = 0
if item = Man hours, format = 0.##
if item = Electricity, format = G4

Because of the dynamic nature of the number format, using something like DisplayFormat(DataFormatString = "{0:d}")] is not appropriate (unless you know of a way to change this at run-time).

However I need to system to 'think' in whatever format is chosen, so if the format is '0.00' then I need all display formats, validations, number rounding etc as '0.00'.

I can store the relevant format of each item in the database.

Many thanks in advance.

John Mackerras
  • 525
  • 1
  • 4
  • 10
  • "G4" is a string so can never be saved as a Decimal, in any format. So you'll have to use a string as the type of the property. After that, well you can try to auto-detect the format (perhaps using regular expressions) after the user finishes entering it, and then use the detection to run a custom section of validation code as appropriate. If it's not possible to automatically distinguish between different formats, you'll have to get the user to select the format they're entering from a pre-defined list of supported formats, so you can use that to choose the correct validation etc. – ADyson Jun 05 '18 at 09:49

1 Answers1

0

You cannot add actual attributes (they are burned into the IL). A possible workaround could be to create your own custom Attribute that implements all the possible validations, display formats etc; and add that attribute at runtime (like this).

Ajay Gupta
  • 1,775
  • 1
  • 10
  • 22