Fastest way to convert number to String

forum.advanced.programming (1998-2005)
Karthik

Fastest way to convert number to String

Post by Karthik » 16 Feb 2005, 22:20

 Posted by:  Karthik 

Main problem is

1)I dont know the number of decimal places
2)I cant put an arbitary number in the number of decimal places.

That is 12.1222 should be converted to 12.1222 in string format. I dont
want a value like 12.12220000 although both of them are mathematically equal

I know that we can convert 12.1222 to a string value like 12.122200000
using SalNumberToStr and then take off the zeros in the end. But thats
slow and not very efficient.

In short, is there a SalNumberToStr where we dont have to specify the
number of decimal places?. Unless my brain is out for lunch (Very much
possible), there is no easy way to do it.

I never really understood the purpose of the decimal character in
SalNumberToStrX. Also, is there an easy way to find the number of
decimal places in a number?

Let me know what you think is fastest, even if it takes 2-3 lines of Code.

Regards
Karthik

Karthik

Re: Fastest way to convert number to String

Post by Karthik » 16 Feb 2005, 22:28

 Posted by:  Karthik 

The best i could come up with is

VisStrSubstitute(SalFmtFormatNumber (12121212, ''),',',STRING_Null)

Gerhard Achrainer

Re: Fastest way to convert number to String

Post by Gerhard Achrainer » 16 Feb 2005, 22:38

 Posted by:  g.achrainer 

Karthik,

I had the same problem. For up to 8 decimals I use:

While SalNumberMod (p_nNumber * SalNumberPower (10, nDecimalPlaces), 1) AND
(nDecimalPlaces < 8)
Set nDecimalPlaces = nDecimalPlaces + 1
Return SalNumberToStrX (p_nNumber, nDecimalPlaces)

I don't know if it's really better than replacing the "0", but at least it's
a bit more "mathematical" *g*

Regards,

gerhard

arvindram11
United States of America
Posts: 555
Joined: 14 Apr 2019, 23:42
Location: Richmond, VA

Re: Fastest way to convert number to String

Post by arvindram11 » 16 Feb 2005, 22:58

 Posted by:  Aravind Ram 

Try SalFmtFormatNumber (df1,
'#.############################################')

The only drawback with the above is a number like 1234.567800 will be
formatted as 1234.5678 (it will omit the 0s at the end)

Aravind

User avatar
Peter.Hugk
Germany
Posts: 375
Joined: 06 Mar 2017, 07:48
Location: Germany

Re: Fastest way to convert number to String

Post by Peter.Hugk » 17 Feb 2005, 08:10

 Posted by:  Dr. Peter Hugk 

Or, for unlimited count of decimals:

Set nNumberTemp = p_nNumber
While SalNumberMod( nNumberTemp, 1 ) > 0
Set nNumberTemp = nNumberTemp *10
Set nDecimalPlaces = nDecimalPlaces +1
Return SalNumberToStrX( p_nNumber, nDecimalPlaces )

Best regards,
Peter

Lubos Vnuk

Re: Fastest way to convert number to String

Post by Lubos Vnuk » 17 Feb 2005, 11:33

 Posted by:  Lubos Vnuk 

this is similar to what I use.

There are 2 problems here though:
1/ http://www.guptaworldwide.com/DevCenter/Tips_2003april.aspx
2/ It requires a bit of work to always force a decimal point "." regardless
of Regional Settings.

HTH,
Lubos.

User avatar
markus.essmayr
Austria
Posts: 892
Joined: 06 Mar 2017, 06:07
Location: Austria

Re: Fastest way to convert number to String

Post by markus.essmayr » 17 Feb 2005, 15:23

 Posted by:  Markus Eßmayr 

Hi,

I tried the following:

Code: Select all

!!CB!! 81
Library name: msvcrt.dll
 ThreadSafe: No
 Function: sprintf
  Description:
  Export Ordinal: 0
  Returns
  Parameters
   Receive String: LPSTR
   String: LPCSTR
   Number: DOUBLE

Code: Select all

!!CB!! 28
On SAM_AppStartup
 Call SalStrSetBufferLength( thevalue, 100 )
 Call sprintf( thevalue, '%.20g', 1232.55353535345345 )
 Call SalMessageBox( thevalue, 'thevalue', MB_Ok )
But you'll also have the problem with the regional settings, I think!

Max

micsto
Germany
Posts: 997
Joined: 07 Mar 2017, 16:07
Location: Germany

Re: Fastest way to convert number to String

Post by micsto » 17 Feb 2005, 17:29

 Posted by:  Michael Stoll \( MICSTO \) 

Fastest in C:

Code: Select all

int WINAPI NumGetScale( NUMBER n )
{
 int iScale = 0;

 if ( n.numLength > 0 )
 {
  int iExp;

  if ( n.numValue[0] & 128 )
  {
   // Positive
   iExp = n.numValue[0] - 192;
   if ( ( iScale = ( n.numLength - 1 - iExp ) * 2 ) > 0 )
   {
    if ( n.numValue[n.numLength - 1] % 10 == 0 )
     --iScale;
   }
   else
    iScale = 0;
  }
  else
  {
   // Negative
   iExp = ( 255 - n.numValue[0] ) - 192;
   if ( ( iScale = ( n.numLength - 2 - iExp ) * 2 ) > 0 )
   {
    if ( n.numValue[n.numLength - 2] % 10 == 0 )
     --iScale;
   }
   else
    iScale = 0;
  }
 }

 return iScale;
}
Michael

Karthik

Re: Fastest way to convert number to String

Post by Karthik » 17 Feb 2005, 21:12

 Posted by:  Karthik 

Cant write so much or call a DLL for such a simple thing :wink:.


Maybe it can be incorporated into SalStrToNumber. If -1 is passed in
number of decimal places, then convert as it is. I hope this will be
incorporated into the next version of GTD.

Cheers
Karthik

Return to “advanced.programming”

Who is online

Users browsing this forum: [Ccbot] and 0 guests