I have an application where users can set how values are displayed. The users enter a formatting string and the component uses FormatFloat to display the value.

But now we are using a new third-party component which formats values using the Format function and of course none of our user formats work as the Format & FormatFloat functions use a different syntax.

So does anyone know of a way of converting between the two? Or maybe someone has code to do it?

2 Answers
2

Though the format strings for FormatFloat can be more or less transformed to ones for Format, you may only get real similarity for positive values. The Format method simply doesn't offer enough flexibility to incorperate the features and fine-grained control that the format strings for the FormatFloat method offers.

For example, the FormatFloat method allows for three different formats for positive, negative and zero values. Also, the FormatFloat format strings allow for string literals, e.g. '#,##0.00;;Zero'; (which means that zero values are printed as "Zero").

To get something similar using the Format function, you yourself would need to do all the grunt work that FormatFloat is doing for you through the format string.

So, although I am as opposed to changing third party control's sources as I am opposed to changing the vcl sources, I am with David on this one: find a way to make the third party control use the FormatFloat function. Preferably through a custom descendant or through an interposer class (also known as an interceptor class), but if that fails, by all means change the third party control's source. Just make sure that you mark the changed sections properly so you can easily redo it when switching to a new version of that control.

+1 I agree with your sentiment about changing 3rd party source; in reality it invariably has to be done. Revision control is your friend in this endeavour (as it is on so many other occasions).
–
David HeffernanFeb 14 '11 at 15:03

@David: indeed. There is no life for a programmer without revision control...
–
Marjan VenemaFeb 14 '11 at 15:31

Far and away the simplest solution will be to take the source of the 3rd party component (you should only consider using 3rd party Delphi components that come with source) and modify it to call FormatFloat rather than Format.