Home General
New Blog Posts: Merging Reports - Part 1 and Part 2

Formatting text fields like numbers

edited April 2004 in General
I've got a database that has a text field that stores data. Sometimes it's
text and sometimes it's a number. If I use a numeric display format like
$#,0.00;($#,0.00) , the field looks like the "$ ,0.00;($ ,0.00)" -- it
appears to take it as text from the field type and keep it that way. Is
there anyway to convert it to a numeric type and format it?

Since I'm doing this with the end-user reporting tool that is going to
accompany our app, I don't really want to use code.

Thanks!

Brad

Comments

  • edited April 2004
    Hi Brad,

    ReportBuilder uses the FormatMaskText method to format any text you have in
    your report. There may be a way you can tweek your display format to allow
    numerical formatting (I believe using 9's instead of 0's) in your text
    fields. Otherwise you may need to take a look at the ReportBuilder source
    in the ppDisplayFormat.pas file. Inside the Format function is where all
    text and numerical formatting is done. You could possibly change this code
    to work for your specific needs.

    Note: If you do decide to change the ReportBuilder code, be sure not to
    change any of the Interface section or RAP will no longer work correctly.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2004
    Nico Cizik (Digital Metaphors) wrote:


    Can you give me an example? Are these formats documented somewhere? I
    can't find a list of allowed formats and what they mean. For example, the
    help file says:

    "The DisplayFormat property is used to control how the data is formatted for
    the report. For example, '#,0' would display 10000 as 10,000..."

    Why? That isn't at all obvious to me.


    I would like to think that I wouldn't have to change source code to format
    text like a number.
  • edited April 2004
    FormatMaskText is a Delphi String Formatting routine. Full documentation on
    how to use it is in the Delphi help file. You should not have to change any
    ReportBuilder source to get the output you need, but if you do run into a
    limitation of how the Delphi formatting functions work... it is a
    possibility.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
  • edited April 2004
    Nico Cizik (Digital Metaphors) wrote:


    I think the limit is that FormatMaskText is used for formatting Strings --
    what does ReportBuilder use to format numbers FormatFloat? It would appear
    then that you cannot then format a string field as a number without
    resorting to code or changing the field type -- True? [I'd check the
    source, but I don't have it yet -- we've not made a determination on a
    reporting tool.]

    Brad
  • edited April 2004
    Hi Brad,

    This is correct. The FormatMaskText method is generally used to format
    Strings... like 1234567890 to (123) 456-7890. I think your best option in
    this case would be to convert your string value to a number before
    formatting (granted you know the record value is numerical). You will want
    to perhaps use the StrToFloat method to make the conversion. Once this
    conversion is done, you should be able to define the Display Format as you
    need $#,0.00;($#,0.00) and the number will print correctly.

    Example...

    if (IsNumerical(Value)) then
    begin
    StrToFloat(Value);
    ppVariable1.DataType := dtDouble;
    ppVariable1.DisplayFormat := '$#,0.00;($#,0.00)';
    end
    else
    ppVariable1.DataType := dtString;

    ppVariable1.Value := Value;

    - IsNumerical is a method you create to check to see if the given string
    only contains the '+-', '0-9', and '.' characters.

    --
    Best Regards,

    Nico Cizik
    Digital Metaphors
    http://www.digital-metaphors.com
This discussion has been closed.