Difficulty Converting Large Float Variable Either to Integer or String

Started by mcjathan, March 15, 2015, 02:34:12 PM

Previous topic - Next topic

mcjathan

I have a Winbatch variable x that is of type Float and whose value is: 4400063251.000000

I want to convert it either to an integer: 4400063251

or convert it to a string: "4400063251"

When I try this:

    x = Int(x)

I get this Winbatch error message:

    3057: Variable could not be converted to a valid number

    Argument number 1 could not be converted

How can I do either of these conversions?

Regards,

Jeff

....IFICantBYTE

I think that number is just too big for the standard functions to work with properly (I think it's just over 2 billion where it will fail) .. you might be able to use the Huge Math extender to do what you want or maybe some fancy other way using .net/com/ole perhaps?
Regards,
....IFICantBYTE

Nothing sucks more than that moment during an argument when you realize you're wrong. :)

JTaylor

If you only want the first part perhaps try using ItemExtract()

int_part = ItemExtract(1,x,".")

It will convert it to a text type I think avoid the number issue.  Couldn't test as I wasn't sure how you were generating the number and just assigning it to a variable didn't seem to generate the desired result.

Jim

td

Quote from: mcjathan on March 15, 2015, 02:34:12 PM
I have a Winbatch variable x that is of type Float and whose value is: 4400063251.000000

I want to convert it either to an integer: 4400063251

or convert it to a string: "4400063251"

When I try this:

    x = Int(x)

I get this Winbatch error message:

    3057: Variable could not be converted to a valid number

    Argument number 1 could not be converted

How can I do either of these conversions?


You cannot convert a number like 4400063251.000000 to a WIL variable of type integer because the integer approximation of the floating point number is greater than the maximum value that can be represented in a 32-bit signed integer.  That means you will need to confine the integer approximation of your large floating point number to a string. 

There are multiple ways to force the value of a large floating point number into a string.   However there is a problem that must be addressed when performing the conversion.  WinBatch automatically converts any floating point number with more than 9 significant digits to scientific notation when converting the number to a string.  While scientific notation works fine for floating point math operations, it makes a mess of trying to extract the integer part of the string representation of a floating point number.   

One workaround is to take advantage the WIL variant variable type by doing something like the following:

f = 4400063251.100000
vt_bs = ObjectType("BSTR", f)
s = ItemExtract(1, vt_bs, '.')
Pause(f, s)

If you need to perform math operations on your large integer then the Huge Math Extender is your friend.
"No one who sees a peregrine falcon fly can ever forget the beauty and thrill of that flight."
  - Dr. Tom Cade