Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How can I do two simple calculations? 3

Status
Not open for further replies.

delphiman

Programmer
Dec 13, 2001
422
0
0
ZA
Problem A

Code:
tblThingTotSqMtrs.Value := (tblThingHight.Value * tblThingWidth.Value);

This results in a number such as 43.6743 whilst I need it to be 43.67

Problem B
Code:
tblThingCost.Value := (tblThingTotSqMtrs.Value * tblThingCostPerSqMtr.Value);

This won't even compile and results in exception
Code:
 codeIncompatible types: 'TCurrencyField' and 'Extended'


What am I missing?
 
problem A :

use RoundTo function :

RoundTo(1.234, -2) will be 1.23

problem B :

use this function
FloatToCurr(const Value: Extended): Currency;

cheers


--------------------------------------
What You See Is What You Get
 
Thanks. :) But you don't show how one APPLIES that information - using the above code?

Are you saying I should declara a function RoundTo?
If so what is it supposed to look like? The following isn't going to work because "1.234" is meaningless.

Code:
function RoundTo(1.234, -2);

Similarly
Code:
function FloatToCurr(const Value: Extended): Currency;

...won't compile because I get exception
";" expected but '.' found.
 

function RoundTo is in unit Math (D6 or later)

function FloatToCurr is in unit SysUtils

 
Lovely!

Now can someone tell me how to USE that information IN THE CODE which I have provided?
 
>function RoundTo is in unit Math (D6 or later)
>function FloatToCurr is in unit SysUtils

So ...?

How does that help me? How do I apply that in my code above?
 
You mast add Math in the uses. Then you can use the functions as whosrdaddy said, without any other declarations.

Giovanni Caramia
 
gcaramia

If you look carefully you will see that "RoundTo(1.234, -2) will be 1.23" as referred to be whosrdaddy is quite meaningless in my code Since I don't HAVE a 1.234 to "RoundTo". I have a FIELD VALUE tblThingTotSqMtrs.Value ... which could be ANYTHING. Including 1.234.

Having added Math to my uses Clause has done nothing for me.

Any more ideas?
 
Your code should look something like:-
Code:
...
Uses Math;
...
tblThingTotSqMtrs.Value := RoundTo ( tblThingHight.Value * tblThingWidth.Value, -2 );
...

Andrew
Hampshire, UK
 
If you can't get your head around passing a variable into a function then I really would advise you to do some simpler stuff until you get used to programming.

"RoundTo(1.234, -2) will be 1.23"

Have a look at that for a second, it's taking the 1.234 and rounding it to have two decimal places, giving the 1.23. The value you want to pass in instead of 1.234 is tblThingHight.Value * tblThingWidth.Value, where do you think you should put that value?

I'm not deliberately being patronising here, just trying to show you how to approach things like this.
 

towerbase

Thanks Andrew! As usual you are a credit to this great website.

KempCGDR

>If you can't get your head around passing a variable into >a function then I really would advise you to do some >simpler stuff until you get used to programming.

I apologize for not having been involved in "simpler stuff until you get used to programming" and troubling this website in this manner.

I have only been in IT since 1965 during which time I have merely "got my head around" programming in Cobol,(Sharp) Basic, MS-QuickBasic, BorlandTurboBasic, MS-FoxPro,
BorlandDBaseIV and Delphi since Delphi1 was released and in fact can and do lecture on Delphi6 and Interbase. Not to mention the delopment of a proposed aircraft maintenance system for ANSET Airlines (before they collapsed due to poor maintenance), Boeing, British Aerospace and Arospatialle and Dassault (France), Qantas, the FAA
(Federal Airline Authority USA), DCA (Dept of Civil Aviation in South Africa) and CASA (Civil Aviation Authority in Australia).

From which meagre experience I have ALREADY managed to solve my problem. The purpose for this thread having been merely an attempt at saving some precious time in the development and re-engineering of a complete accounting system for the 4th time (using Delphi this time)since 1984.

Once again I apologize for having taken up the time of someone of your stature and as experienced as you and I remain grateful for your "trying to show you [me] how to
approach things like this." :) :)

By the way .... your question "where do you think you should put that value?"

How does "tblThingTotSqMtrs.Value" grab you? Try reading my code again .... PROPERLY! :)

 
By the way .... your question "where do you think you should put that value?"

How does "tblThingTotSqMtrs.Value" grab you? Try reading my code again .... PROPERLY!

I think, if you read your own code again .... PROPERLY! you will see that the value in question should in fact be placed as the first parameter of the RoundTo function thus giving you a formatted result that can be be put into tblThingTotSqMtrs.Value.
 
Please don't quote all that experience at me in the same thread where you fail to understand the depth and complexity of the roundTo function, it merely makes me despair for our IT industry.

Seriously though, I do think that this thread has dragged on for quite a while longer than it should have so in the interests of not starting a flame war I'm going to apologise for any offense I may have caused you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top