AJ.
AJ.

Reputation: 11240

Delphi FormatFloat and Format functions

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?

Thanks,

AJ

Upvotes: 3

Views: 2338

Answers (2)

Marjan Venema
Marjan Venema

Reputation: 19346

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.

Upvotes: 3

David Heffernan
David Heffernan

Reputation: 612954

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.

Upvotes: 2

Related Questions