Decimal detection

Decimal detection

Postby Marc Vanzegbroeck » Mon Sep 27, 2021 1:22 pm

Hi,

I want to convert some values from a database to a text file for export use.

Normaly a use str() to convert the value.

The problem is that if the field is define with 5 Decimals, and the value is 4.204, the export is 4.20400.
Then I can use something like str(10,3).
Is there a easy way to know the amount of decimals used? So when the value is 4.20000 is -return 4.2, 4.25600, return 4.256?
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Decimal detection

Postby FranciscoA » Mon Sep 27, 2021 4:13 pm

Hi.
WhatsNew Fwh2106 contains this:
* Función FW_ExcelToDBF(): Elimina los ceros a la derecha después del decimal al
convertir números a texto.

Maybe it can help you.
Regards
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2158
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Decimal detection

Postby James Bott » Mon Sep 27, 2021 4:34 pm

Code: Select all  Expand view
Function RemoveZeros( cValue )
   do while right(cValue,1) = "0"
      cValue:= left(cValue, len(cValue) -1)
   enddo
Return cValue
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Decimal detection

Postby FranciscoA » Mon Sep 27, 2021 5:57 pm

Code: Select all  Expand view
function RemoveRightZeros(cString)
  local nPos := 0, n, nLen := Len(cString)

  if ValType(cString) <> "C"
     MsgStop("El valor recibido debe ser del tipo Cadena de texto", "Alto")
     Return ""
  endif

  FOR n := 1 TO nLen
     if RIGHT(cString,n,1) <> "0"
        nPos := nLen - (n-1)
        exit
     endif
  END

RETURN MsgInfo( SUBSTR(cString, 1, nPos), cString )
 
Francisco J. Alegría P.
Chinandega, Nicaragua.

Fwxh-MySql-TMySql
User avatar
FranciscoA
 
Posts: 2158
Joined: Fri Jul 18, 2008 1:24 am
Location: Chinandega, Nicaragua, C.A.

Re: Decimal detection

Postby Marc Vanzegbroeck » Mon Sep 27, 2021 7:24 pm

James, Francisco,

Thank you, they both are working fine.
The only thing was that if there was a value without decimal digits it returns the value with a dot.

Like '122.00000' returns '122.'

I will optimize it with testing if the return-value ends with '.', if so, I will return it without the '.'
Regards,
Marc

FWH32+xHarbour | FWH64+Harbour | BCC | DBF | ADO+MySQL | ADO+MariaDB | ADO+SQLite
Marc Vanzegbroeck
 
Posts: 1159
Joined: Mon Oct 17, 2005 5:41 am
Location: Belgium

Re: Decimal detection

Postby James Bott » Mon Sep 27, 2021 8:30 pm

Marc,

Here it is with the new revisions you mentioned.

James

Code: Select all  Expand view
/*
Purpose  :  Reformat number in string format
Program  :
Author   : James Bott, jbott@compuserve.com
Date     :
Company  : Intellitech
Language : Fivewin/xHarbour
Updated  :
Notes    :

*/


#include "fivewin.ch"

Function Main()

   Local cValue:= "122.000"
   
   msgInfo( RemoveZeros(cValue) )
   
   cValue := "122.220"
   
   msgInfo( RemoveZeros(cValue) )
   
   quit
   
Return nil

// Reformat number in string format (cValue)
Function RemoveZeros( cValue )

   // Remove trailing zeros
   do while right(cValue,1) = "0"
      cValue:= left(cValue, len(cValue) -1)
   enddo
   
   // Remove trailing decimal point
   if right(cValue,1) = "."
      cValue:= left(cValue,len(cValue)-1)
   endif
   
Return cValue
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Decimal detection

Postby nageswaragunupudi » Mon Sep 27, 2021 9:43 pm

Code: Select all  Expand view

cNum := Str( .... )
cTrim := If( "." $ cNum, RemRight( RemRight( cNum, "0" ), "." ), cNum )
 


OR
Simply use the FWH built-in function
Code: Select all  Expand view

cNum := cNumToStr( nValue, .f., .f. )
 

instead of using cNum := Str( nValue )
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Decimal detection

Postby James Bott » Mon Sep 27, 2021 10:47 pm

Nages,

cNum := cNumToStr( nValue, .f., .f. )

Wow, one line, I like that! Less is more.

I note that in my version of FWH 1805, it is not listed in the documentation, but the function is in version 1805. You might want to check to make sure it is in the documentation for the latest version.

Please tell us what the 3rd and 4th parameters are.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Decimal detection

Postby nageswaragunupudi » Mon Sep 27, 2021 11:00 pm

Code: Select all  Expand view
cNumToStr( nVal, [lEuropean], [lThouSep] )

2nd and 3rd parameters default to settings by FWNumFormat(...)
Regards

G. N. Rao.
Hyderabad, India
User avatar
nageswaragunupudi
 
Posts: 10620
Joined: Sun Nov 19, 2006 5:22 am
Location: India

Re: Decimal detection

Postby James Bott » Sat Oct 09, 2021 6:53 pm

Marc,

... '122.00000' returns '122.'


I should point out that technically 122.00000 is not the same as 122.

"122.00000" implies that it is accurate to the nearest 5th decimal place (one hundred thousandth of a unit). And 122 implies it is accurate to the nearest whole number. I know that database number formats aren't setup to deal with this issue so it is something to keep in mind.

James
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA

Re: Decimal detection

Postby James Bott » Sat Oct 09, 2021 7:12 pm

Marc,

I should have included this in my previous message. Below is the info for the Round() function from the Clipper manual. Depending on the real accuracy of the numbers you are converting, this should be used to get the proper whole number for your translation.

James

ROUND()
Return a numeric value rounded to a specified number of digits
------------------------------------------------------------------------------
Syntax

ROUND(<nNumber>, <nDecimals>) --> nRounded

Arguments

<nNumber> is the numeric value to be rounded.

<nDecimals> defines the number of decimal places to retain.
Specifying a negative <nDecimals> value rounds whole number digits.

Returns

ROUND() returns a numeric value.

Description

ROUND() is a numeric function that rounds <nNumber> to the number of
places specified by <nDecimals>. Specifying a zero or negative value
for <nDecimals> allows rounding of whole numbers. A negative
<nDecimals> indicates the number of digits to the left of the decimal
point to round. Digits between five to nine (inclusive) are rounded up.
Digits below five are rounded down.

The display of the return value does not obey the DECIMALS setting
unless SET FIXED is ON. With SET FIXED OFF, the display of the return
value contains as many decimal digits as you specify for <nDecimals>, or
zero, if <nDecimals> is less than one.

Examples

. These examples round values with decimal digits:

SET DECIMALS TO 2
SET FIXED ON
//
? ROUND(10.4, 0) // Result: 10.00
? ROUND(10.5, 0) // Result: 11.00
? ROUND(10.51, 0) // Result: 11.00
? ROUND(10.49999999999999, 2) // Result: 10.50

. These examples use a negative <nDecimals> argument to round
numeric values to whole number values:

? ROUND(101.99, -1) // Result: 100.00
? ROUND(109.99, -1) // Result: 110.00
? ROUND(109.99, -2) // Result: 100.00

Files Library is CLIPPER.LIB.
FWH 18.05/xHarbour 1.2.3/BCC7/Windows 10
User avatar
James Bott
 
Posts: 4840
Joined: Fri Nov 18, 2005 4:52 pm
Location: San Diego, California, USA


Return to FiveWin for Harbour/xHarbour

Who is online

Users browsing this forum: Google [Bot] and 78 guests