Round Function

Round Function

Postby Colin Haig » Mon Feb 15, 2010 9:08 am

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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function

Postby Willi Quintana » Mon Feb 15, 2010 3:30 pm

Hi, you can use this command:

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

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

Re: Round Function

Postby James Bott » Mon Feb 15, 2010 9:04 pm

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
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Round Function

Postby Willi Quintana » Tue Feb 16, 2010 1:50 am

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)
User avatar
Willi Quintana
 
Posts: 1003
Joined: Sun Oct 09, 2005 10:41 pm
Location: Cusco - Perú

Re: Round Function

Postby Colin Haig » Tue Feb 16, 2010 2:09 am

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)

Postby Colin Haig » Tue Feb 16, 2010 6:29 am

Thanks I have solved my problem.
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function

Postby Antonio Linares » Tue Feb 16, 2010 9:05 am

Colin,

How did you solve it ? :-)

thanks,
regards, saludos

Antonio Linares
www.fivetechsoft.com
User avatar
Antonio Linares
Site Admin
 
Posts: 41390
Joined: Thu Oct 06, 2005 5:47 pm
Location: Spain

Re: Round Function

Postby Colin Haig » Tue Feb 16, 2010 11:05 pm

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
Colin Haig
 
Posts: 310
Joined: Mon Oct 10, 2005 5:10 am

Re: Round Function

Postby xProgrammer » Thu Feb 18, 2010 3:02 am

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
User avatar
xProgrammer
 
Posts: 464
Joined: Tue May 16, 2006 7:47 am
Location: Australia


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Gale FORd and 67 guests