Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

how can i make a float upto 6 decimal places?

Posted on 2004-09-28
15
Medium Priority
?
972 Views
Last Modified: 2010-08-05
My problem is when i assign a value with my currency:float;

currency:=1.123456;

the currency variable will only hold upto 4 decimal places even if i already set the "CurrencyDecimals=6"
the currency above will automatically change the value with "1.1235".

anybody can help?

0
Comment
Question by:girlswants_me
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 5
  • 3
  • +1
15 Comments
 
LVL 30

Expert Comment

by:third
ID: 12176917
try this,

Currency := StrToFloat(FormatFloat('0.000000', 1.123456));
0
 

Author Comment

by:girlswants_me
ID: 12177303
Im using this DB float

procedure TfDataModule.CreateTable_CurrencyExchangeTable(NameFile: string);
var
 DatabaseDirectory:string;
begin
  with TTable.Create(Application) do
  begin
    Active := False;
    DatabaseName := fMain.p_sDatabasePath;
    TableName := NameFile;
    TableType := ttDBase;
    with FieldDefs do
    begin
      Clear;
      Add('INDEXNO'         , ftString , 15, false);
      Add('CURRDESC'        , ftString ,  5, false);
      Add('DISPCURR'        , ftString ,  1, false);
      Add('EXCVALUE'        , ftfloat  ,  0, false);
    end;
    CreateTable;
    Free;
  end;
end;

Now when the 1.1234567 will be assigned to the field "EXCVALUE", when you browse the ""EXCVALUE" field again the number shown will already 1.1235.

0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12177978
The currency type isn't a true float type. It's an Integer type where the numbers are just shifted 4 positions. Basically, it's an "Int64 div 1000" type. Thus you can NEVER get more than 4 decimals in a currency type, just like you cannot get any decimal in an integer type.

The currency type is actually just a fixed-decimal type. Use Extended instead if you need more decimals and remember, if it's ever gets converted to currency and back again, you'll lose 2 digits. There is no cure for this, though. Except for not using currency at all.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12178677
Thus: Use Extended, not currency!
0
 
LVL 17

Expert Comment

by:geobul
ID: 12180971
Hi,

1. Use AddFieldDef method instead of Add
2. Use ftBCD instead of ftFloat
3. Set Precision property of the field to the desired size
4. Set Size property to the desired number of digits following the decimal point, i.e. 6 in your case

Regards, Geo
0
 

Author Comment

by:girlswants_me
ID: 12187570
geobul,

Can you please write this in codes..... What i am using now is saving it as string and when i want to use that decimal number i just turn it to strtofloat(decimalString)...

0
 
LVL 17

Accepted Solution

by:
geobul earned 150 total points
ID: 12187760
Sure:

procedure TfDataModule.CreateTable_CurrencyExchangeTable(NameFile: string);
var
 DatabaseDirectory:string;
begin
  with TTable.Create(Application) do
  begin
    Active := False;
    DatabaseName := fMain.p_sDatabasePath;
    TableName := NameFile;
    TableType := ttDBase;
    with FieldDefs do begin
      Clear;
      Add('INDEXNO'         , ftString , 15, false);
      Add('CURRDESC'        , ftString ,  5, false);
      Add('DISPCURR'        , ftString ,  1, false);
      with AddFieldDef do begin
        Name := 'EXCVALUE';
        DataType := ftBCD;
        Precision := 15;
        Size := 6;
        Required := false;
      end;
    end;
    CreateTable;
    Free;
  end;
end;
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12188209
@geobul, I've checked something. The TBCDField type uses the currency type for it's Value property. And no matter what you try, the currency type will NEVER hold more than 4 decimals after the dot. This might be a minor flaw. A TFloatField would be better. The use of ftFloat would be better, don't you agree? Because ftFloat uses a Double type for the value.

But the flaw might actually be in the DBase table that is used. If in this table, the precision of the field is set to 4 positions then you'll never get 6 positions in it...

@girlswants_me, can you check the table definition of your DBase table, to make sure it uses 6 decimals after the dot? You can easily do this from the Database Desktop. If the field itself doesn't hold more than 4 positions then that's the problem...

And avoid the currency type. It's not usable if you want more than a precision of 4.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12190096
Well, it's been a long time ago when I used BDE for the last time. As far as I remember using ftBCD was the way for making DBase field of type 'Numeric' and with fixed length after the point (that field in my example above should become 'N 15 6' in Database Desktop - in my D5 at least). Thinking more now, ftFloat should be capable of storing 6 digits and more also.

So, perhaps the problem is not in the table layout but in the type of the var used for reading (as Alex already pointed out) and/or in the visualization method being used. For the last I'd suggest Format function:

var currency: float; // or double/extended
begin
  ... // open the table and position it to a record
  currency := Table1.FieldByName('EXCVALUE').AsFloat;
  ShowMessage(Format('%15.6n',[currency])); // you must get '1.123456' here if you had that value in the table.

Regards, Geo
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12190151
@geobul, when I read the currency:float part of the post, it just suddenly struck me. girlswants_me is using a VARIABLE called currency, of type float. He is not using the currency TYPE. And I remembered that with DBase, you have to specify the number of decimal places of any numeric field. Thus, this is why his table itself has the flaw. I think EXCVALUE is probably defined as a Float field with only 4 digits after the dot. Which means the flaw is in his table, which can be solved by making the EXCVALUE slightly bigger. (Requires a restructure.)

I dare to bet 500 points that this is a table structure issue. :-)
0
 
LVL 17

Expert Comment

by:geobul
ID: 12191515
@Workshop_Alex, I thought it was a db problem in the beginning too. That's why I posted comments about 'improving' CreateTable code. But reconsidered my opinion later on. I'm accepting your bet just for fun :-)

@girlswants_me, we are waiting your response with great interest.
0
 
LVL 17

Expert Comment

by:geobul
ID: 12191690
Hey, there is no FLOAT type for variables. What type are you using actually?
0
 
LVL 17

Expert Comment

by:Wim ten Brink
ID: 12193858
Either Double or Extended I guess. Both types are referred as FLOAT types, but the Extended type is 2 bytes bigger. With Delphi, the FLOAT type in databases is converted to the Double type.
0
 

Author Comment

by:girlswants_me
ID: 12224063
Hi everyone,

Sorry for this late answer. Actually i tried geobul's code and it works fine for me. Actually, i am using the "currency" as type ftString, and when ever i will get the value of my "currency" i will just make it as a float ex. curr = strtofloat(thetable.fieldbyname('currency').asstring)

thank you very much for all your support and help
0
 
LVL 17

Expert Comment

by:geobul
ID: 12235149
0

Featured Post

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses

670 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question