I have writed this piece of code in java:
String[] tokens = strLine.split(delims);
SimpleOrderRequest bet = new SimpleOrderRequest();
long Id = Long.parseLong(tokens[1].trim());
byte polarity = Byte.parseByte(tokens[2].trim());
float price = Float.parseFloat(tokens[3].trim());
float stake = Float.parseFloat(tokens[4].trim());
bet.selectionId = Id;
bet.polarity = polarity;
bet.stake = new BigDecimal(stake) ;
bet.price = new BigDecimal(price) ;
Where tokens[1],tokens[2],tokens[3],tokens[4] are numbers in different formats. The problem is that i have to cut the numbers to the first digit after the comma in order to send the data to wsdl service. Namely, when the the string that i read is:
0.00426, 12955094, 1, 100., 1.
0.00050, 12954726, 1, 100., 1.
all works fine and bet.stake=1. and bet.price=100. But when the string is in the form:
0.00154, 13189533, 1, 2.10, 34.77
then bet.stake=34.770000457763671875 and bet.price=2.099999904632568359375 , and the wsdl service don’t allow me to place the request, so i have to cut the number. Can i do this directly when i am parsing the numbers? if no what i have to do?
You shouldn’t be using
floatto represent currency values in the first place. Just use:(etc)
It looks like you’ve got the right data type within
SimpleOrderRequest– so why would you introduce a binary floating point conversion?It’s important that you understand how binary floating point works – there are umpteen questions on Stack Overflow about it. Basically, you should normally use
floatanddoublefor “natural” values such as height and weight, where magnitude is important but there’s really no such thing as a precise value – and useBigDecimalfor “artificial” values where you can certainly have exactly $1.10 for example.