Page 1 of 1

Decimal detection

PostPosted: Mon Sep 27, 2021 1:22 pm
by Marc Vanzegbroeck
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?

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 4:13 pm
by FranciscoA
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

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 4:34 pm
by James Bott
Code: Select all  Expand view
Function RemoveZeros( cValue )
   do while right(cValue,1) = "0"
      cValue:= left(cValue, len(cValue) -1)
   enddo
Return cValue

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 5:57 pm
by FranciscoA
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 )
 

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 7:24 pm
by Marc Vanzegbroeck
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 '.'

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 8:30 pm
by James Bott
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

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 9:43 pm
by nageswaragunupudi
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 )

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 10:47 pm
by James Bott
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.

Re: Decimal detection

PostPosted: Mon Sep 27, 2021 11:00 pm
by nageswaragunupudi
Code: Select all  Expand view
cNumToStr( nVal, [lEuropean], [lThouSep] )

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

Re: Decimal detection

PostPosted: Sat Oct 09, 2021 6:53 pm
by James Bott
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

Re: Decimal detection

PostPosted: Sat Oct 09, 2021 7:12 pm
by James Bott
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.