I am Android developer.I get from web service result 45.0000.i am stored ArrayList.But i want show it point after two digit only.
Example output 45.00 only .Please give me solution.
Use DecimalFormat class to achieve this.
DecimalFormat df=new DecimalFormat("##.##");
String formate = df.format(value);
double finalValue = (Double)df.parse(formate) ;
you can use String.format, it returns a formatted string using the specified format string and arguments
String.format("$%.2f", value);
also String.format uses your Locale
int index = 0;
for (; index < yourList.size (); index++) {
String tmpvalue = yourList.get(index);
String conValue = String.format("$%.2f", Float.parse(tmpvalue));
yourList.set(index, conValue);
index++;
}
Related
I've try this in my device and work fine. But, in some Android device, the symbol is in wrong place. This is my code :
public static String convertToRupiah(String priceBeforeConverted){
//manual setting separator, because currently RUPIAH is NOT supported
DecimalFormat formatter = (DecimalFormat) DecimalFormat.getCurrencyInstance();
DecimalFormatSymbols formatRupiah = new DecimalFormatSymbols();
formatRupiah.setCurrencySymbol("Rp ");
formatRupiah.setMonetaryDecimalSeparator(',');
formatRupiah.setGroupingSeparator('.');
formatter.setDecimalFormatSymbols(formatRupiah);
Double price = StringFormatter.isNullOrEmpty(priceBeforeConverted) ? 0.00 : Double.valueOf(priceBeforeConverted) ;
String conversionResult = formatter.format(price);
if(conversionResult.endsWith(",00"))
conversionResult = conversionResult.substring(0, conversionResult.length()-3);
return conversionResult;
}
expected result is : Rp 25.000,00
String pattern = "Rp ###,###.000 ";
DecimalFormat decimalFormat = new DecimalFormat(pattern);
String format = decimalFormat.format(25.000);
System.out.println(format);
--Try this, your zeros after decimal place will not miss. Output of this code is.
Rp 25.000
Let me know if anything is not clear.
I have a string named namely "-10.00","-100.00","-1000.00". I want to get value like "10","100","1000" from that string. I have tried to get substring but did not able to get.
code i have tried
String amount = "-10.00";
String trimwalletBalance = amount.substring(0, amount.indexOf('.'));
From above i only get "-10".
String trimwalletBalance = amount.substring(1, amoun.indexOf("."));
Its very simple.
Do it like String trimwalletBalance = amount.substring(1, amount.indexOf('.'));
Instead of position 0, You should get substring from position 1
Convert it into integer and then name it positive:
String amount = "-10.00";
int amountInt = (int) Double.parseDouble(amount);
if(amountInt<0)amountInt*=-1;
Try
String amount = "-10.00";
int value = (int) Double.parseDouble(amount);
if(value < 0) value *= -1;
//value will be 10
OR
String text = amount.substring(1, amount.indexOf('.'));
How to display unicode character in TextView in Android using RubyMotion?
I tried
MainModule.get.setText('\u00d7')
MainModule.get.setText('0x00d7')
Where get is to get the TextView object.
But neither works, it still displays the original string(\u00d7 or 0x00d7).
Edit
I am using Ruby to write Android, not Java.
I get you a method that unicode to string, I hole that would be useful for you.
mTextView.setText(unicode2String("\\u00d7"));
...
public String unicode2String(String unicode) {
StringBuffer string = new StringBuffer();
String[] hex = unicode.split("\\\\u");
for (int i = 1; i < hex.length; i++) {
int data = Integer.parseInt(hex[i], 16);
string.append((char) data);
}
return string.toString();
}
I'm using google volley to retrieve source code from website. Some looping was done to capture the value in the code. I've successfully captured the data I wanted, but error was shown: NumberFormatException: Invalid float: "2,459.00"
My intention was to store the value after the class=ListPrice>
Sample:
RM 2,899.00
The example value of the source code I wanted to save is "RM2,459.00 "
Below is the code I've written:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lazada_result);
lelongResult = (TextView) findViewById(R.id.lelong_result);
RequestQueue lelong = MyVolley.getRequestQueue(this);
StringRequest myLel = new StringRequest(
Method.GET,
"http://list.lelong.com.my/Auc/List/List.asp?DA=A&TheKeyword=iphone&x=0&y=0&CategoryID=&PriceLBound=&PriceUBound=",
RetrieveLelong(), createMyReqErrorListener());
lelong.add(myLel);
}
private Response.Listener<String> RetrieveLelong() {
return new Response.Listener<String>() {
#Override
public void onResponse(String response) {
ArrayList<Float> integers = new ArrayList<>();
String to = "class=ListPrice>";
String remainingText = response;
String showP = "";
while (remainingText.indexOf(to) >= 0) {
String tokenString = remainingText.substring(remainingText
.indexOf(to) + to.length());
String priceString = tokenString.substring(0,
tokenString.indexOf("<"));
float price = Float.parseFloat(priceString.replaceAll("[^\\d,]+", "").trim());
integers.add((price / 100));
remainingText = tokenString;
}
for (int i = 0; i < integers.size(); i++) {
String test1 = Float.toString(integers.get(i));
showP += test1 + "\n";
}
lelongResult.setText(showP);
}
};
}
The problem was as below:
I've tried all sort of replaceAll(),
1)replaceAll("[^\d,]+","") result:2,89900
replace all character except digits and comma works.
2)replaceAll("[^\d]+","") result:Invalid int""
replace all character include comma and dot ,not working
3)replaceAll("[^\d.]+,"") result:Invalid int""
replace all character exclude digits and dot, not working
From the experiment 2&3 coding above,I've noticed that if the comma were removed,i cant parseFloat as the value received by it is: "".NumberFormatException:Invalid Float:"" shown.
From the experiment 1,NumberFormatException:Invalid Float "2,45900" is showned.
The problem was replacing comma ,the code will not work but with the presence of comma ,the value cannot be stored into string
try this:
float price = Float.parseFloat(priceString.replaceAll("RM", "").trim());
use `replaceAll(Pattern.quote(","), "");
EDIT
if you want only numbers then use this
String s1= s.replaceAll("\D+","");
Try to parse the number by specifying the Locale.
NumberFormat format = NumberFormat.getInstance(Locale.KOREAN);
Number number = format.parse(priceString.replaceAll("RM", ""));
double d = number.doubleValue();
I'm just guessing the locale, don't know what you should use, depends on country
You need to do it one by one
priceString=priceString.replaceAll("\\D", "");
priceString=priceString.replaceAll("\\s", "");
now
priceString=priceString.trim();
float price = Float.parseFloat(priceString);
the problem is that in your code:
priceString.replaceAll(Pattern.quote(","), "");
float price = Float.parseFloat(priceString.replaceAll("\\D+\\s+", "").trim());
You are replacing coma but not storing the value!
you have to do:
priceString = priceString.replaceAll(",", "");
float price = Float.parseFloat(priceString.replaceAll("\\D+\\s+", "").trim());
I'm not sure of the pattern "\D+\s" because if you remove the coma you don't need to replace anything else (except "RM" that i assume you already removed)
Update: set locale and parse a number:
NumberFormat format = NumberFormat.getInstance(Locale.KOREAN);
Number number = format.parse(priceString.replaceAll("RM", ""));
double d = number.doubleValue();
I have Edittext field that the user can enter their name. I want to check it with alphabets for how many times a letter comes.
Which is the best way to do this?
I tried the below code for getting each character from the string but error occurring? I appreciate it if any one can help.
name=(EditText)findViewById(gami.Numerology.R.id.inputUI);
String a=name.getText().toString();
for ( int i = 0; i < a.length(); i++ )
{
c = a.charAt(i);
if (c=='s')
{
s=1; //like this for all characters
}
}
I just put this in a click event of button.
//declare the original String object
String strOrig = "Hello World";
//declare the char array
char[] stringArray;
//convert string into array using toCharArray() method of string class
stringArray = strOrig.toCharArray();
//display the array
for(int index=0; index < stringArray.length; index++)
//check of character appearance
You can use a hashmap or some sort of dictionary in Java.
Rough example since I don't know how to use Hashmap in Java:
HashMap<Character, Integer> counter = new HashMap<Character, Integer>();
for (Character c: name)
{
counter.put(c, counter.get(c) == null ? 1 : counter.get(c)++);
}