How to convert long string to number?


#1

Hello, I have 34 long string, how to convert it to long number ?

thanks :slight_smile:


#2

Hi,

How about like this?
CLng(integerstring.Trim)
or
Convert.ToLong(integerstring.Trim)

If there are Alphas or Specials it will throw an error so keep that in mind.

to get around error you could use these examples:

If(IsNumeric(integerstring.Trim),CLng(integerstring.Trim),integerstring.Trim)

or replace the Alphas
CLng(System.Text.RegularExpressions.Regex.Replace(text, “[^0-9]”, “”))

Thankyoo


#3

Thanks, but it doesn’t work…
“Value was either too large or too small for a Decimal”


#4

Change CLng or ToLong to CDbl or ToDouble

Longs are integers so you might need to use Double instead to account for decimals.


#5

34 digits is quite too much for an integer type: Long can only have a value up to 9,223,372,036,854,775,807, being 19 digits. You will have to work with floating point numbers (e.g. Double) and suffer a loss of precision, or with something like BigInteger.


#6

This is my string "249041911436190623612713252128"
Using windows calculator -> scientific mode
249041911436190623612713252128 mod 97 = 1
and that’s fine

CDbl gives me 24 :confused:


#7

Try BigInteger

image


#8

BigInteger solved the problem :slight_smile: thanks :slight_smile:


#9

Hello Vaidya,

May I Know What Code You Have taken in Variable Val(BigInteger.Parse…) May I Know the Code

Please I’m a Learner… Done with the Course

Regards,
Ganesh


#10

If you are asking about below part, it is BigInteger.Parse(strval)

image


#11

Thank You Vaidya,

Really So Nice of You. Recently I done my Course, Going to Learn real time cases by forum…

if it is possible could you please help me with some real time cases Pleaseeeeee

Ganesh


#12

I learned a long time ago that there was Integer and Long types. So it was just CInt() or CLng()
… C for convert
CLng(strval)

I don’t know if there is a difference but that’s what I use. And, it’s usually a good idea to use Long for Invoice numbers, etc.


#13

Hi @ClaytonM,

Cint - Returns the Int32
CLng- Returns the Int64

The only main difference is the value range.

Regards
Balamurugan


#14

@claytonm just to note your if would not work because integerstring.trim is not of the same type as CLNG output :S so you probably want to write “Nothing” instead


#15

Thanks for the info @balupad14
I meant, I didn’t know if there was a difference between using BigInteger.Parse() and CLng()


#16

Hi,
From my understanding, the string just needs to have all numerical values (and same for BigInteger.Parse()), so you don’t necessarily need to use a regex.

CLng(integerstring.Trim) would work if it is all numbers.
Typically, though one must check if it’s a number or it will throw an error.

If( IsNumeric(integerstring.Trim), CLng(integerstring.Trim), 0 )

So you can use 0 if the value coming in is wrong. But you can also use Regex, like you pointed out, if you want to extract only the number out from a alpha-numerica string. — depends on the goals of the project I suppose.

Thanks for pointing that out!