简体   繁体   中英

Dynamically set number format for Decimal

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.

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 ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM