How can i get both float and string in output? - java

How can I obtain the output 6214.07500000sec
I am reading this data from the file and trying to insert in a table. I am getting an error because of string error .
I have tried to parse:
float t_f = Float.parseFloat(td)
Doesn't help.
Please give a suggestion for getting both string and floating number.

def output = '6214.07500000sec'
Map data = (output=~/(\d+(\.\d+)?)(.*)/).find{true}.with{ m->
return [ value: m[1] as Float, measure: m[3] ]
}
println data
result:
[value:6214.075, measure:sec]

You can't store the full String in a float since you got the "sec" at the end, one solution could be as #notyou said in the comments to store everything in a string, another solution could be to separate the letters from the numbers, here's a short code that will do the trick:
String output = "6214.07500000sec";
String letters;
float number;
Scanner scan = new Scanner(output);
number = Float.parseFloat(scan.findInLine("\\d+(\\.\\d+)?"));
letters = scan.next();
System.out.println(number);
System.out.println(letters);
this code prints:
6214.075
sec
if you want all the zeros you have to store it as a String.

Related

Java Object format to String Number with Decimals

so I have an array list of Objects and in it are string numbers. I want to add decimal places to these numbers (8).
String value = String.valueOf(accountEntry.get(4));
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%.8f",amount);
accountEntry.add(formatted);
For example 101700000000 should output 1017 but instead it is 101700000000,00000000
Does anyone know where the problem is?
Hello try something like this using Regex , this way you can remove all zeros at the end.
String value = String.valueOf("101700000000");
double amount = Double.valueOf(value);
String formatted = String.format(Locale.GERMANY,"%d",(long)amount);
formatted = formatted.replaceAll("0+$", "");
System.out.println(formatted);
Input :101700000000 ===> Output: 1017
Does anyone know where the problem is?
Your Input is : 101700000000 and you are formatting String.format(Locale.GERMANY,"%.8f",amount); In here your output will be 101700000000,00000000 So for understand this String Format will not transform your input magicaly to 1017. You need to use another algorithm for this problem

How to convert a string with multiple points to double in android

I have a string "3,350,800" with multiple points I want to convert to double but have error multiple points
String number = "3,350,800"
number = number.replace(",", ".");
double value = Double.parseDouble(number);
Error : java.lang.NumberFormatException: multiple points
The . character is used as a decimal point in English, and you cannot have more than one of those in a number.
It seems like you're using it as a thousands separator though. This is legal in several locales - you just need to use one that allows it, e.g.:
String number = "3.350.800";
NumberFormat format = NumberFormat.getInstance(Locale.GERMAN);
double value = format.parse(number).doubleValue();
Mix of other answers, no reason to change the , for . and then fetch the German local.
String number = "3,350,800";
NumberFormat format = NumberFormat.getInstance();
double value = format.parse(number).doubleValue();
System.out.println(value);
Output:
3350800.0
you need to use something like this :
String number = "3,350,800";
number = number.replaceAll(",", "");
double value = Double.parseDouble(number);
System.out.println(value);
What number are you trying to get?
3.350.800 is what you're trying to parse as a double,
but that's obviously not a number, since there are "multiple points".
If you just wanna get 3,350,800 as your number, simply change this line -
number = number.replace(",", ".");
to this -
number = number.replace(",", "");

Number format exception for decimal number when given double

When I run the following script to check inputQty greater than AvailQty I am getting the following:
java.lang.NumberFormatException: For input string: "97,045.1193"
This error occurs if availableQty is a decimal number. The value is delivered from a database, can you please correct where I am wrong?
double AvailQty = Double.valueOf(AQTY.getValue());
double inputQty = Double.valueOf(QTY.getValue());
if(inputQty > AvailQty){
session.setStatusMessage("Not Enough Quantity");
//Abort operation
throw new AbortHandlerException();
}
Thanks
Your string contains commas, remove any commas before parsing.
Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",", ""));
It can't format it because it doesn't accept the comma. You could do something like Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",", "")) and Double.valueOf(XX_IGL_QTY.getValue().replaceAll(",", "")) to remove any commas before parsing.
Simple, your string contains commas. This is not legal. All you can have are numbers and a dot (decimal separator).
I don't know where you get the value from, but if it's not something you can change on that side, you will have to do some hacking ;)
Double.valueOf(INV.AVAILQTY.getValue().replaceAll(",","");
Rather than re-formatting the string to do the conversion, you can tell the formatter to read the numbers using whatever format you like.
This answer shows how to change the formatting.
Here's a sample test to show how the same number in your question can be parsed successfully:
#Test
public void showNumberFormats() throws ParseException {
String rawAvailQty = "97,045.1193";
String rawInptQty = "98,045.3421";
NumberFormat nf = NumberFormat.getNumberInstance();
double AvailQty = nf.parse(rawAvailQty).doubleValue();
double inputQty = nf.parse(rawInptQty).doubleValue();
if(inputQty > AvailQty){
//Abort operation
System.err.println("Could not perform operation");
}
System.out.println("Available qty: " + AvailQty);
System.out.println("Input qty: " + inputQty);
}
Prints:
Could not perform operation
Available qty: 97045.1193
Input qty: 98045.3421

Parsing string to double/float throwing errors

I am extracting couple of values like 1234, 2456.00 etc from UI as string. When I try to parse this string to float, 1234 is becoming 1234.0 and when I tried to parse as double its throwing error. How can I solve this?
I am using selenium web driver and java. Below are few things I tried.
double Val=Double.parseDouble("SOQ");
double Val=(long)Double.parseDouble("SOQ");``
I think you mixed it up a bit when trying to figure out how to parse the numbers. So here is an overview:
// lets say you have two Strings, one with a simple int number and one floating point number
String anIntegerString = "1234";
String aDoubleString = "1234.123";
// you can parse the String with the integer value as double
double integerStringAsDoubleValue = Double.parseDouble(anIntegerString);
System.out.println("integer String as double value = " + integerStringAsDoubleValue);
// or you can parse the integer String as an int (of course)
int integerStringAsIntValue = Integer.parseInt(anIntegerString);
System.out.println("integer String as int value = " + integerStringAsIntValue);
// if you have a String with some sort of floating point number, you can parse it as double
double doubleStringAsDoubleValue = Double.parseDouble(aDoubleString);
System.out.println("double String as double value = " + doubleStringAsDoubleValue);
// but you will not be able to parse an int as double
int doubleStringAsIntegerValue = Integer.parseInt(aDoubleString); // this throws a NumberFormatException because you are trying to force a double into an int - and java won't assume how to handle the digits after the .
System.out.println("double String as int value = " + doubleStringAsIntegerValue);
This code would print out:
integer String as double value = 1234.0
integer String as int value = 1234
double String as double value = 1234.123
Exception in thread "main" java.lang.NumberFormatException: For input string: "1234.123"
Java will stop "parsing" the number right when it hits the . because an integer can never have a . and the same goes for any other non-numeric vales like "ABC", "123$", "one" ... A human may be able to read "123$" as a number, but Java won't make any assumptions on how to interpret the "$".
Furthermore: for float or double you can either provide a normal integer number or anything with a . somewhere, but no other character besides . is allowed (not even , or ; and not even a WHITESPACE)
EDIT:
If you have a number with "zeros" at the end, it may look nice and understandable for a human, but a computer doesn't need them, since the number is still mathematically correct when omitting the zeros.
e.g. "123.00" is the same as 123 or 123.000000
It is only a question of formatting the output when printing or displaying the number again (in which case the number will be casted back into a string). You can do it like this:
String numericString = "2456.00 "; // your double as a string
double doubleValue = Double.parseDouble(numericString); // parse the number as a real double
// Do stuff with the double value
String printDouble = new DecimalFormat("#.00").format(doubleValue); // force the double to have at least 2 digits after the .
System.out.println(printDouble); // will print "2456.00"
You can find an overview on DecimalFormat here.
For example the # means "this is a digit, but leading zeros are omitted" and 0 means "this is a digit and will not be omitted, even if zero"
hope this helps
Your first problem is that "SOQ" is not a number.
Second, if you want create a number using a String, you can use parseDouble and give in a value that does not have a decimal point. Like so:
Double.parseDouble("1");
If you have a value saved as a long you do not have to do any conversions to save it as a double. This will compile and print 10.0:
long l = 10l;
double d = l;
System.out.println(d);
Finally, please read this Asking a good question
The problem is you cannot parse non-numeric input as a Double.
For example:
Double.parseDouble("my text");
Double.parseDouble("alphanumeric1234");
Double.parseDouble("SOQ");
will cause errors.
but the following is valid:
Double.parseDouble("34");
Double.parseDouble("1234.00");
The number you want to parse into Double contains "," and space so you need first to get rid of them before you do the parsing
String str = "1234, 2456.00".replace(",", "").replace(" ", "");
double Val=Double.parseDouble(str);

Calculate variable String(010) with integer(1) where outcome is string(011)

The user can insert an random string that can only contains numbers.
But it must been possible to calculate it with an integer.
the problem is the user insert a string like 010 + integer(1) it would result in 11;
but i want to return a string 011
But the user can also enter numbers like 001, 0001, etc
What is the best approach here? I have try to use
String.format("%05d", yournumber); but i does not work with variable strings
I also came across
String str = "abcd1234";
String[] part = str.split("(?<=\\D)(?=\\d)");
System.out.println(part[0]);
System.out.println(part[1]);
If i use it like this i got a new problem.
how to split the number in a right way.
Any idea what i'm missing
Considering you are handling Integer number (not binary )
Try something like this:
String input ="001";//your user input
/**
* your check here if input is a number
*/
int len=input.length();
int inputInteger=Integer.parseInt(input);
inputInteger+=1;
String output=String.format("%0"+len+"d", inputInteger);
System.out.println(output);

Categories