I'm getting below error while converting String array to Long array.
java.lang.NumberFormatException: Invalid long: "5571.329849243164". How to round off this value like 5571.
Use this
String.format("%.2f", d)
//try this way, hope this will help you...
String value = "5571.329849243164";
Toast.makeText(MyActivity.this,""+Math.round(Double.parseDouble(value)),Toast.LENGTH_SHORT).show();
round(Double value)`
see here
String string = "5571.329849243164";
Double value1 = Double.parseDouble(string);
tv.setText(Math.round(value1) + "/");
I understand you want to convert a String representing a floating point value to an Integer with rounding, not truncation. Perfectly reasonable.
Two choices.
Convert the string to a Double first (Double.valueOf()), then convert to Integer (intValue()) using a rounding algorithm. Usually it's enough to just add 0.5 to the Double.
Round the String using string operations, then convert to Integer. Split the string at the first ".", convert it to Integer (getInteger()). If the digit after the "." is in the range 5-9 then add one.
I'm sure you can write the code.
Related
I have double amount. Let amount to be 500000.12. I want to set value of the amount to TextView like this format 500,000.12 (where 12 is cents , 500,000 is dollars).
I wrote this function and it works
private String getAmountAsString(double amount) {
double integralPart = amount % 1;
int fractionalPart = (int) (amount - integralPart);
int integral = (int) integralPart * 100;
String strFractional = String.format("%,d", fractionalPart);
String strAmount = (strFractional + "." + String.valueOf(integral));
return strAmount;
}
But I think that there can be some easy and good way of doing this with java native functions. Can anybody help to find functions or some better way?
various Locale can be used to format float, double etc. You can use:
String.format(Locale.<Your Locale>, "%1$,.2f", myDouble);
Here .2f represents how many digits you want after decimal. If you are not specifying any locale it will use the default locale.
In String class this method is overloaded as:
format(String format, Object... args)
&
format(Locale l, String format, Object... args)
For more info have a look: Link1 , Link2 and Link3
Therefore NumberFormats are used. They are good to handle local differents for different countries.
//define a local, gets automated if a point or comma is correct in this Country.
NumberFormat anotherFormat = NumberFormat.getNumberInstance(Locale.US);
DecimalFormat anotherDFormat = (DecimalFormat) anotherFormat;
anotherDFormat.applyPattern("#.00");//set the number of digits afer the point
anotherDFormat.setGroupingUsed(true);// set grouping
anotherDFormat.setGroupingSize(3);//and size of grouping
double myDouble = 123456.78;
String numberWithSeparators = anotherDFormat.format(myDouble);//convert it
I think you have to take a look at this one
http://docs.oracle.com/javase/tutorial/i18n/format/decimalFormat.html
I'm getting price from server in this format 1299.0000 or 4399.000 how i will remove all zero after "." help me please how i will do that how to remove numbers after"." ???? im getting form server price value infloating number how i will remove all zero after "."
how to remove zero after decimal like 13400.0000 (remove last 4 zero)
or 2349.00 (remove last 2 zero)
remove all zero after "." how I do that?
static ArrayList<String> Category_price= new ArrayList<String>();
Category_price.add(object.getString("price"));
holder.txtText3.setText("Price: "+Html.fromHtml(ProductList.Category_price.get(position)));
Thanks
convert it into integer type..............................................
oR
Split the string, use decimal point(.) as a spliter character.
OR
String val=Html.fromHtml(ProductList.Category_price.get(position));
String val1=val.substring(0,val.indexOf("."));
holder.txtText3.setText("Price: "+val1);
try this way..
BigDecimal bd = new BigDecimal(23.086);
BigDecimal bd1= new BigDecimal(0.000);
DecimalFormat df = new DecimalFormat("0.##");
System.out.println("bd value::"+ df.format(bd));
System.out.println("bd1 value::"+ df.format(bd1));
Not only for Bigdecimal for decimal also..
Try this
String[] separated = CurrentString.split(".");
separated[0]; // this will contain "price"
separated[1]; // this will contain " all your zeros"
Try this:
String price = object.getString("price");
String price = price.substring(0, price.indexOf("."));
If your string is always in that format (2 digits, minus, 2 digits, minus, 4 digits, space etc...) then you can use substring(int beginIndex, int endIndex) method of string to get what you want.
Note that second parameter is the index of character after returning substring.
I want to ask how can i convert from hex to decimal in Java ?
I tried using parseInt by getting the number from the user in an edit text and view the result in a text view after splitting the long spaced number but its not working
any ideas ?
String s=et.getText().toString();
String[] F=s.split(" ");
String last=F[F.length-1] ;
int i=Integer.parseInt(last, 16);
tv1.setText(i);
You're close... just make sure to convert i to a string like this:
tv1.setText(i + "");
Hope this helps :)
This is baffling me. I am grabbing a String and converting it to a Char array but the resulting characters are not the same as the original String. What gives? I've tried it one character at a time as well as trying toCharArray(). Same results.
Output:
07-21 09:58:27.700: V/meh(22907): Loaded String = [C#42126d88
07-21 09:58:27.700: V/meh(22907): Convert to Char = [C#41693070
String temp = prefManager_.getString("PrevGameState", "");
Log.v("meh", "Loaded String = " + temp);
pieceStates_ = temp.toCharArray();
Log.v("meh", "Convert to Char = " + pieceStates_.toString());
The value it outputs is not a string indeed, it's a pointer in memory. Probably you are not overriding the toString() method or there is something wrong.
The fact that the two pointers are not the same doesn't mean that the two strings are not equal (which should be compared with .equals(..) and not in any different way).
To be more precise, if pieceStates_.toString() prints [C#41693070 then the toString is not overridden and Java doesn't know how to print it. Same thing applies to the other variable. Then an array type in Java is not printable by default, you should use Arrays.toString(..) to actually see its content.
Use :
System.out.println("Convert to Char = " + String.valueOf(pieceStates_) );
String.valueOf(Character_Array)
Above method converts it back to String object.
How to convert double to a string value that I get from the spinner, for example: "10 (ton)."
In summary:
How to convert string to double and delete the part (ton)?
I need only the value 10
Another option is using Java's substring method in the String class.
The signature is:
substring(int beginIndex, int endIndex)
Where endIndex equals to the index of the last character you want to include + 1.
In the case of your example, it will look like this:
String myString = "10 (ton)";
Double dbl = Double.parseDouble(myString.substring(0, 2));
Here is the link to the method:
Java substring
You must parse this String. Here is some example. Also use search.
Check these methods Double.parseDouble() and Double.toString() use these functions for converting double to string or vice-versa.
first you have to get rid of "(ton)" which can be achieved by using a String method for example
String inputString = "10 (ton)";
String str = inputString.split(" ")[0];
After that just parse the double Value
Double dbl = Double.parseDouble(str);
BTW: Not sure whether you want to go from double to string or vice-versa
i have a similar problem.
for correct formatting EditText text content to double value i use this code:
try {
String eAm = etAmount.getText().toString();
DecimalFormat dF = new DecimalFormat("0.00");
Number num = dF.parse(eAm);
mPayContext.amount = num.doubleValue();
} catch (Exception e) {
mPayContext.amount = 0.0d;
}
this is independet from current phone locale and return correct double value.
hope it's help;