Jump to content


Photo

FormatMask in C++ Builder


  • Please log in to reply
9 replies to this topic

#1 LOLe

LOLe
  • Members
  • 18 posts

Posted 04 September 2006 - 06:34 PM

Hi,


I am creating columns (typ=number) in run-time and I want to set FormatMask to
the column I have created.

I tried to use following line (no format mask).
TNxNumberColumn(NextGrid1->Columns->Item[0]).FormatMask="###0.00";
Have tried different

If I create the columns at design-time and use the NxNumberColumn1->FormatMask="###0.00"; then
I have the FormatMask.
I can not create the columns at design, I must create them at run-time

Is there someone who knows to set FormatMask to columns created in run-time?

I am using C++ Builder 6 Pro with latest update.

Lars-Olof

#2 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,195 posts
  • Gender:Male

Posted 05 September 2006 - 05:42 AM

Hello Lars,

Please try to set it to #.##0.00 (add decimal separator after first #)

regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#3 LOLe

LOLe
  • Members
  • 18 posts

Posted 05 September 2006 - 07:13 PM

QUOTE (Boki (Berg) @ Sep 5 2006, 06:42 AM) <{POST_SNAPBACK}>
Hello Lars,

Please try to set it to #.##0.00 (add decimal separator after first #)

regards


Did not work.

My test aplication has 2 numerical columns.
When I use FormatMask on the first column using NxNumberColumn1->FormatMask="###0.00";
I get numbers with 2 decimals 5 will be 5.00, 3.325 are 3.33.

Using TNxNumberColumn(NextGrid1->Columns->PositionItem[1]).FormatMask="#.##0.00"; to
set the FormatMask for the second column.
There numbers in the column are as follow 5 are 5, 3.326 are 3.326.
It does not matter if I use Item or PositionItem.

As I can understand, to set FormatMask at run-time in Delphi the code below should be used.
CODE
uses NxColumns, NxColumnClasses;

TNxNumberColumn(NextGrid1.Columns[4]).FormatMask := '#0.00';


In C++ Builder I can not use Columns[4] (NextGrid1->Columns[4]) I need to use Item or PositionItem.
I think the problem are the using Item/PositionItem. (Only guessing).

Any other ideé how I can set FormatMask at run-time?
Is it hard to change the code so it would be possibly to use: NextGrid1->Columns->PositionItem[1]->FormatMask="###0.00";

For the moment I am using text columns, but using text columns I will loose decimals when only showing
2 decimals.

Hope you can help me!

Lars-Olof

#4 LOLe

LOLe
  • Members
  • 18 posts

Posted 05 September 2006 - 08:57 PM

Hi,

I tried one more thinag and that was this:
TNxNumberColumn(NxNumberColumn2).FormatMask="###0.00";
Did not work. This column was created at design-time and I tried to set the FormatMask
at run-time.

There is nor problem to compile no warnings and no error.

Hope this will help you to find where the problem are.

How will this look in Delphi?

Lars-Olof

#5 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,195 posts
  • Gender:Male

Posted 05 September 2006 - 10:06 PM

Hello Lars,

I am using standard Delphi formating functions. I recommend that you read FormatFloat function for more info.

QUOTE
Formats a floating point value.

Unit

SysUtils

Category

floating point conversion routines

Delphi syntax:

function FormatFloat(const Format: string; Value: Extended): string; overload;
function FormatFloat(const Format: string; Value: Extended; const FormatSettings: TFormatSettings): string; overload;

C++ syntax:

extern PACKAGE AnsiString __fastcall FormatFloat(const AnsiString Format, Extended Value);
extern PACKAGE AnsiString __fastcall FormatFloat(const AnsiString Format, Extended Value, const TFormatSettings FormatSettings);

Description

FormatFloat formats the floating-point value given by Value using the format string given by Format. The following format specifiers are supported in the format string:

Specifier Represents
0 Digit place holder. If the value being formatted has a digit in the position where the '0' appears in the format string, then that digit is copied to the output string. Otherwise, a '0' is stored in that position in the output string.
# Digit placeholder. If the value being formatted has a digit in the position where the '#' appears in the format string, then that digit is copied to the output string. Otherwise, nothing is stored in that position in the output string.
. Decimal point. The first '.' character in the format string determines the location of the decimal separator in the formatted value; any additional '.' characters are ignored. The actual character used as a the decimal separator in the output string is determined by the DecimalSeparator global variable or its TFormatSettings equivalent.
, Thousand separator. If the format string contains one or more ',' characters, the output will have thousand separators inserted between each group of three digits to the left of the decimal point. The placement and number of ',' characters in the format string does not affect the output, except to indicate that thousand separators are wanted. The actual character used as a the thousand separator in the output is determined by the ThousandSeparator global variable or its TFormatSettings equivalent.
E+ Scientific notation. If any of the strings 'E+', 'E-', 'e+', or 'e-' are contained in the format string, the number is formatted using scientific notation. A group of up to four '0' characters can immediately follow the 'E+', 'E-', 'e+', or 'e-' to determine the minimum number of digits in the exponent. The 'E+' and 'e+' formats cause a plus sign to be output for positive exponents and a minus sign to be output for negative exponents. The 'E-' and 'e-' formats output a sign character only for negative exponents.
'xx'/"xx" Characters enclosed in single or double quotes are output as-is, and do not affect formatting.
; Separates sections for positive, negative, and zero numbers in the format string.
The locations of the leftmost '0' before the decimal point in the format string and the rightmost '0' after the decimal point in the format string determine the range of digits that are always present in the output string.
The number being formatted is always rounded to as many decimal places as there are digit placeholders ('0' or '#') to the right of the decimal point. If the format string contains no decimal point, the value being formatted is rounded to the nearest whole number.

If the number being formatted has more digits to the left of the decimal separator than there are digit placeholders to the left of the '.' character in the format string, the extra digits are output before the first digit placeholder.
To allow different formats for positive, negative, and zero values, the format string can contain between one and three sections separated by semicolons.

One section: The format string applies to all values.
Two sections: The first section applies to positive values and zeros, and the second section applies to negative values.
Three sections: The first section applies to positive values, the second applies to negative values, and the third applies to zeros.

If the section for negative values or the section for zero values is empty, that is if there is nothing between the semicolons that delimit the section, the section for positive values is used instead.

If the section for positive values is empty, or if the entire format string is empty, the value is formatted using general floating-point formatting with 15 significant digits, corresponding to a call to FloatToStrF with the ffGeneral format. General floating-point formatting is also used if the value has more than 18 digits to the left of the decimal point and the format string does not specify scientific notation.

The first form of FormatFloat is not thread-safe, because it uses localization information contained in global variables. The second form of FormatFloat, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of FormatFloat, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call GetLocaleFormatSettings.

boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#6 bosjo

bosjo
  • Members
  • 75 posts
  • Gender:Male
  • Location:Märsta, Sweden

Posted 05 September 2006 - 10:12 PM

Hej Lars-Olof,

I'm not a C++ programmer, so my comment may be stupid, but it seems to me that you simply want to cast the column to the correct type, and I think that is done in a slightly more complicated way in C++, something like this:

((TNxNumberColumn)NextGrid1->Columns->Item[0]).FormatMask="###0.00";

#7 LOLe

LOLe
  • Members
  • 18 posts

Posted 05 September 2006 - 10:31 PM

Hi bosjo,

I had come up with a soultion before I read your 2 last posts.

The idea you had in the last post did not (I tested), could not compile.
The solution that I get to work are as follow:

CODE
    TNxNumberColumn* column = (TNxNumberColumn*)  NextGrid1->Columns->PositionItem[1];

    column->FormatMask="###0.00";


About your first idea. I was using FormatFloat but the problem with FormatFloat are that I losing decimals.
I imported decimal numbers like 3.326 and with FormatFloat I get 3.33 as a string (what I want).
Now I want to sum all the values in one column and I want to sum the value 3.326 and not 3.33.

But thanks for the help and I hope my solution will be to help for the C++ users of NextGrid.

Thanks again for the help.

Lars-Olof

#8 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,195 posts
  • Gender:Male

Posted 06 September 2006 - 02:59 AM

Hello Lars,

FormatFloat is used internally inside NextGrid, and you may simply use same rulles when setting FormatMask of NumberColumn.

Simply read help article about FormatFloat and you will get it all.

regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.

#9 LOLe

LOLe
  • Members
  • 18 posts

Posted 06 September 2006 - 08:29 AM

QUOTE (Boki (Berg) @ Sep 6 2006, 03:59 AM) <{POST_SNAPBACK}>
Hello Lars,

FormatFloat is used internally inside NextGrid, and you may simply use same rulles when setting FormatMask of NumberColumn.

Simply read help article about FormatFloat and you will get it all.

regards


Hi Boki,

Thanks for the answer.

I will try FormatFloat more and see what it can.

Lars-Olof

#10 Boki (Berg)

Boki (Berg)

    Boki (Berg)

  • Forum Admin
  • PipPipPipPipPip
  • 8,195 posts
  • Gender:Male

Posted 06 September 2006 - 11:42 PM

Hello Lars,

You simply need to apply same rules as in used in FormatFloat.

regards
boki@bergsoft.net | LinkedIn Profile
--
BergSoft Home Page: www.bergsoft.net
Users Section: users.bergsoft.net
Articles and Tutorials: help.bergsoft.net (Developers Network)
--
BergSoft Facebook page
--
Send us applications made with our components and we will submit them on: www.bergsoft.net/apps.htm. Link to this page will be also set on home page too.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users