Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


converting a long integer to a string

 
Logged in as: Guest
arrSession:exec spGetSession 2,2,61158
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> WSH & Client Side VBScript >> converting a long integer to a string
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 converting a long integer to a string - 6/7/2008 6:23:29 AM   
  thorwath


Posts: 42
Score: 0
Joined: 12/6/2006
From: Grand Rapids, MI
Status: offline
I need to get long account numbers from a database and then build an Excel spreadsheet. That
spreadsheet is used by others who need to see the acount number in its long digit format. But when
I attempt to convert the account number to a string, using cStr(), it converts the number into
scientifc notation. The following code produces the message:

1.23456789012346E+15
"1.23456789012346E+15"

The code is:

      
Can anyone suggest a solution I could use to preserve the visual integer when converting it
to a string?

-Thanks, Terry Horwath

 
 
Post #: 1
 
 RE: converting a long integer to a string - 6/7/2008 6:42:50 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
That's because the number is too long for WSH/VBS.

Try this


      



_____________________________

Fred

(in reply to thorwath)
 
 
Post #: 2
 
 RE: converting a long integer to a string - 6/7/2008 10:37:45 AM   
  TNO


Posts: 1066
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
You could do it if you use the Floating point to your advantage:

Dim num: num=1234567890123456/1000
MsgBox Replace(CStr(num),".","")

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to Fredledingue)
 
 
Post #: 3
 
 RE: converting a long integer to a string - 6/7/2008 11:28:02 AM   
  thorwath


Posts: 42
Score: 0
Joined: 12/6/2006
From: Grand Rapids, MI
Status: offline
quote:

ORIGINAL: TNO
'
Dim num: num=1234567890123456/1000
MsgBox Replace(CStr(num),".","")

Looked promising using the number I provided in the original post, but produces incorrect behavior when using the following
numbers:

- any number that has no remainder when divided by 1000
- any number ending in 0

Interestingly/oddly 1234567890123001 is truncated to 1234567890123 when divided by 1000.

From a pragmatic perspective this little trick has a high probability of working correctly. But unfortunately it is not bullet proof.

Here is the code I experimented with:

      

(in reply to TNO)
 
 
Post #: 4
 
 RE: converting a long integer to a string - 6/8/2008 2:45:49 AM   
  TNO


Posts: 1066
Score: 10
Joined: 12/18/2004
From: thenewobjective.com
Status: offline
Time to think outside the box. VBScript just can't handle those bignumbers on its own.

Try using this WSF file:


      

JavaScript can handle the big number no problem, so we can let that do the work.

_____________________________

To iterate is human, to recurse divine. -- L. Peter Deutsch

(in reply to thorwath)
 
 
Post #: 5
 
 RE: converting a long integer to a string - 6/8/2008 5:48:32 AM   
  Fredledingue


Posts: 337
Score: 0
Joined: 5/9/2005
From:
Status: offline
What is the code to get the number from the database?

_____________________________

Fred

(in reply to TNO)
 
 
Post #: 6
 
 RE: converting a long integer to a string - 6/8/2008 2:35:06 PM   
  dm_4ever


Posts: 2433
Score: 38
Joined: 6/29/2006
From: Orange County, California
Status: offline
....another hack that might work....


      

_____________________________

dm_4ever

My philosophy: K.I.S.S - Keep It Simple Stupid
Read Me: http://www.visualbasicscript.com/m_24727/tm.htm
Frequently Asked Stuff: http://www.visualbasicscript.com/m_47117/tm.htm

(in reply to thorwath)
 
 
Post #: 7
 
 RE: converting a long integer to a string - 6/10/2008 5:41:27 AM   
  thorwath


Posts: 42
Score: 0
Joined: 12/6/2006
From: Grand Rapids, MI
Status: offline
Just an FYI... I am only working on this script on the weekends (its a little "side" project), so I will be
trying some of these interesting suggestion next Sunday. Just wanted to make sure that those
who have taken the time to provide ideas do not think I have lost interest! (I moderate a forum
on Mercuy QuickTest Pro and hate it when a good reply seemingly never gets used...)

-Terry

"We all stand on the shoulders of those who have banged on the keyboards before use". So
share, share, share.

(in reply to dm_4ever)
 
 
Post #: 8
 
 RE: converting a long integer to a string - 6/15/2008 12:48:47 PM   
  thorwath


Posts: 42
Score: 0
Joined: 12/6/2006
From: Grand Rapids, MI
Status: offline
DM,

Your suggestion to use the ADODB.Recordset COM object code worked like a charm for my problem.
Account numbers are now beaten into submission! (Well, at least into readable strings...)

The only thing I had to do was strip the unwanted trailing newline that is appended to the string
returned by objRecSet.GetString(), which was easy to do using mid().

Thanks to **ALL** for the many helpful suggestions posted to this thread.

-Terry

(in reply to dm_4ever)
 
 
Post #: 9
 
 RE: converting a long integer to a string - 6/16/2008 5:37:06 AM   
  thorwath


Posts: 42
Score: 0
Joined: 12/6/2006
From: Grand Rapids, MI
Status: offline
For anyone who is interested, my script used to validate DM's workaround is attached. the script
creates an Excel spreadsheet so Excel mut be installed. Should work with any version of Excel,
including 2007.

Thanks again for the help on this.

Attachment (1)

(in reply to thorwath)
 
 
Post #: 10
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> WSH & Client Side VBScript >> converting a long integer to a string Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts