Round Function

Post Reply
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Round Function

Post by Colin Haig »

Hi All

I have a database value of 495.00

Assign to a variable

nPoCost := oPo:cost

MsgInfo(nPoCost) // 495.0000

nPoCost := round(nPoCost,2)

MsgInfo(nPoCost) // 495.0000

nPoCost := val(str(oPo:cost,9,2))

MsgInfo(nPoCost) // 495.0000

I have not set decimals because it is supposed to default to 2

I require nPoCost to be 495.00

Is MsgInfo causing this when it converts a numeric'

Thanks

Colin
User avatar
Willi Quintana
Posts: 859
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú
Contact:

Re: Round Function

Post by Willi Quintana »

Hi, you can use this command:

SET DECIMAL TO 2
nPoCost := round(nPoCost,2)
MsgInfo(nPoCost) // 495.00

regards
User avatar
James Bott
Posts: 4654
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA
Contact:

Re: Round Function

Post by James Bott »

Colin,

It looks like SET DECIMAL is defaulting to 4. Have you tried:

n:= 495

msgInfo( n )

Does this show 495.00 or 495.0000?

Regards,
James
User avatar
Willi Quintana
Posts: 859
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú
Contact:

Re: Round Function

Post by Willi Quintana »

This function was recommended by A.L.


//--------------------------------------------------------------------------------------------------
Function FRound( x, y )
Local nDeci := 2 // error en el compilador
Default y := 2
nDeci := Set(3, y )
If x >= 0
x := Val( Str( Int( x * 10 ** y + 0.5000001 ) ) ) / 10 ** y
Else
x := Val( Str( Int( x * 10 ** y - 0.5000001 ) ) ) / 10 ** y
Endif
Set(3, nDeci ) // set decimal
Return(x)
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function

Post by Colin Haig »

Hi Willi

I tried that function but still the same result - I think the val() function in
xHarbour is different from clipper.

Regards

Colin
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function (Solved)

Post by Colin Haig »

Thanks I have solved my problem.
User avatar
Antonio Linares
Site Admin
Posts: 37481
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain
Contact:

Re: Round Function

Post by Antonio Linares »

Colin,

How did you solve it ? :-)

thanks,
regards, saludos

Antonio Linares
www.fivetechsoft.com
Colin Haig
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function

Post by Colin Haig »

Hi Antonio

I think MsgInfo when diplaying a numeric adds extra decimal points which
confused me -

nRate := oEmp:rate
MsgInfo(nRate) // returned 40.000000

I was trying to pass nRate to SQL insert string which kepted failing but it turned out it
was the way I was passing nRate - the documentation said to pass a numeric value which
worked when I manually entered a value into the string eg ,40.00, but the when i
converted nRate to a string and entered has ,' " + cRate + " ', it worked as well.

I am interfacing my software with an accounting package called MYOB. using ADODB and it has been
quite a learning curve.

Many thanks to James Bott who provided help with this and other issues recently.

Cheers

Colin
User avatar
xProgrammer
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia

Re: Round Function

Post by xProgrammer »

Hi all

I suspect there is a fundamental point to be made here. That when stored as a numeric value in [x]Harbour etc 495.0, 495.00, 495.0000 and Round( 495.0000, 2 ) are all stored identically.

Round( , 2) would change the internally stored value of 495.006 to 495.01 (and be the same as 495.01, 495.0100 etc).

You have to distinguish between the internal storage of the number (which doesn't recognise the number of significant digits) and its output through functions such as MsgInfo(), Str(), Transform etc. which in some cases will do their own rounding.

I also note, from the xHarbour documentation, that in the absence of a SET DECIMALS command the default should be 2, although SET DECIMALS without a value specified sets it to 0. It also notes that you need SET FIXED ON. These two settings affects on screen display - ? and QOut() and the standard say / get system. It has no bearing on the precision of numbers (other than certain advanced mathematical functions).

I haven't looked at the MsgInfo() code, but you can retrieve the value of SET DECIMALS and SET EXACT using the Set() function so it wouldn't be hard to make MsgInfo() SET DECIMALS and SET EXACT aware if you so wished. Remember MsgInfo() is a FWH extension (and a very useful one too) but is not a standard [x]Harbour screen display function.

Regards
xProgrammer
Post Reply