简体   繁体   English

动态设置小数的数字格式

[英]Dynamically set number format for Decimal

I need to change the format of a Decimal number depending on other values. 我需要根据其他值更改Decimal的格式。

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... 替换灯泡-仅整数=数量只能等于1或2或3 ...
Man hours - charged in quarters - Qty could equal 1.25 (2 decimal points) 工时-按季度计费-数量等于1.25(2个小数点)
Electricity used - charged to 4 decimal points = Qty could equal 23.1234 用电量-充电至4个小数点=数量等于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 如果item = Light Globes,格式= 0
if item = Man hours, format = 0.## 如果项目=工时,则格式= 0。##
if item = Electricity, format = G4 如果项目=电力,格式= 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). 由于数字格式的动态性质,因此不宜使用DisplayFormat(DataFormatString = "{0:d}")] (除非您知道在运行时更改此方法的方法)。

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'. 但是我需要系统“思考”所选择的任何格式,因此,如果格式为“ 0.00”,那么我需要所有显示格式,验证,数字舍入等为“ 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). 您不能添加实际属性(它们已刻录到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 ). 并在运行时(如添加属性 )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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