Edit box vs. percentage value display and edit

In my entity, the percent value is held as a decimal number (for clarity: 30% = 0.3). This is used to make various calculations, e.g. regarding margin/mark-up. I would like the user to be able to see this value in the Edit Box (or some other widget, if it has similar functionality but solves this problem) and then change it but still as a percentage - i.e. the user enters e.g. 31.5% and the entity holds this value as 0.315. But at the same time, the user should always see this value expressed as a percentage in the edit box (for clarity: 31.5%). I don't care about displaying the percentage symbol (%, although I admit that it would be nice to be able to do so) in the edit box, but I am wondering how to customise the edit box to present this data correctly so that I don't have to use some helper entity or do some calculation each time (first converting the percentage value from the text '31.5%' to a decimal number to store it in the entity, and then the other way around to display it correctly in the edit box). The alternative, of course, is to store the value expressed as '31.5' in the entity and then, if calculations are needed, divide it by 100, but maybe I don't need to do that after all?
1 answers

Easiest solution is to have 2 attributes

  1. Percentage
  2. CalcPercentage

Use Percentage to be displayed everywhere, edit box, text widget etc..

When editing the value Percentage, set the value CalcPercentage Percentage div 100.

So no hassle with displaying and going back and forth calculating. As I assume that the percentage value isn't continuously changing