NumberFormatException in BigDecimal to int or long(java) - numberformatexception

I am coding in Java,I want to transfer BigDecimal data to int or long, the data name is "recovered",its value is"889.0" and has the structure as follows:
[enter image description here][1]
I tried Long.valueOf(recovered.toString()) and Integer.valueOf(recovered.toString()),but both generate NumberFormatException.
I also tried recovered recovered.toString().getBytes() and I got:
[enter image description here][2]
How can I have int or long value of "889.0"?
Thank you.
[1]: https://i.stack.imgur.com/GF3tD.jpg
[2]: https://i.stack.imgur.com/TXNBb.jpg

The string "889.0" is not a valid format to convert to a Long or an Integer, it is a decimal format and you could convert it to a Float or a Double.
If you want an integer you could convert it to a float first and then use Float.intValue to convert it to an integer.
Float f = Float.valueOf(recovered.toString();
Integer i = f.intValue();

Related

What is the memory difference between serializing a value as string and serializing a value as other data type in protobuf

Let's say I have a variable a, which I want to assign a decimal.
If my proto file is
syntax = "proto3";
message Test{
string a = 1;
}
How much memory will that take and how much will be the difference if I change string a =1 to float a =1.
Is there a documentation where you can see how much memory is assigned to different datatype?

error parsing string to int -java.lang.NumberFormatExceptiont

I'm parsing a String to an Integer and I get the following error:
Exception in thread "main" java.lang.NumberFormatException: For input
string: "9887666881"
code
long accountNo=0;
if(accountNo_p!=null)
accountNo=Integer.parseInt(accountNo_p);
can you please advise?
Java's int size is 32 bits wide. The maximum value for a (signed) 32-bit integer in Java's format is 2,147,483,647. The input string is 9,887,666,881. The input is larger than the maximum value for a Java int, so it throws a NumberFormatException (since you're using the Integer method for parseInt, not a method to parse a long).
Solutions include using smaller numbers (if this is a wild test case, not real data), or using a Java long (via Long.parseLong(accountNo_p)).

How to convert 64 bit binary string to a double float in ruby?

I am wondering how to convert a 64 bit binary string to a double float in ruby. The string that I have is as follows:
binaryString = "0011111111110000000000000000000000000000000000000000000000000000"
Using an online converter (http://www.binaryconvert.com/convert_double.html?) I know that the value should be 1.0. However, I'm attempting to use the ruby unpack to convert to double, and I'm not getting the correct result.
double_value = binaryString.unpack("G")
Gives me double_value = 1.3983819593719592e-76
I've tried other directives like "F" and "D", but none yield correct results.
Any ideas what I am doing wrong? Thank you for the help!
unpack expects binary data, so you have to pack your bit string first using B:
b = '0011111111110000000000000000000000000000000000000000000000000000'
[b].pack('B*').unpack1('G')
#=> 1.0

How to convert HEX value to Decimal in HIVE HQL

I've got Hive 1.1, and I'm having trouble converting a HEX value into a Decimal or BIGINT.
Using UNHEX('2BD1BCAE0501250E') should return= 3157512269357720846
But instead, I'm getting something like WingDings= +Ѽ�%
I've tried DECODE, ENCODE, BASE64...but nothing seems to be working. Has anyone else tried doing this? Thanks
Conv(STRING num, int from_base, int to_base) Converts a number from a given base to another
conv('2BD1BCAE0501250E', 16, 10)

Converting non-decimal numbers to another non-decimal

Not that it's a lot of work, but the only way I know to convert a non-decimal to another non-decimal is by converting the number to decimal first, then take a second step to convert it to a new base. For example, to convert 456 (in base 7) to 567 (in base 8), I would calculate the decimal value of 456, then convert that value into base 8...
Is there a better way to go directly from 7 to 8? or any base to any other base for that matter?
Here's what I have:
//source_lang and target_lang are just the numeric symbols, they would be "0123456789" if they were decimal, and "0123456789abcdef" if hex.
private string translate(string num, string source_lang, string target_lang)
{
int b10 = 0;
string rv = "";
for (int i=num.Length-1; i>=0; i--){
b10 += source_lang.IndexOf( num[i] ) * ((int)Math.Pow(source_lang.Length, num.Length -1 - i));
}
while (b10 > 0) {
rv = target_lang[b10 % target_lang.Length] + rv;
b10 /= target_lang.Length;
}
return rv;
}
You're not really converting into base 10. You're converting it into a numeric data type instead of a string representation. If anything, you're converting it into binary :) It's worth distinguishing between "an integer" (which doesn't intrinsically have a base) and "the textual representation of an integer" (which does).
That seems like a sensible way to go, IMO. However, your conversion routines certainly aren't particularly efficient. I would separate out your code into Parse and Format methods, then the Convert method can be something like:
public static string Convert(string text, int sourceBase, int targetBase)
{
int number = Parse(text, sourceBase);
return Format(number, targetBase);
}
(You can use a string to represent the different bases if you want, of course. If you really need that sort of flexibility though, I'd be tempted to create a new class to represent a "numeric representation". That class should probably be the one to have Parse, Format and Convert in it.)

Resources