I have a String in the format of "6.151536E-8"
How can i convert it to a string or int as 0.000000061 ?
Use this if you want to have just 2 significant digits:
String str = "6.151536E-8";
BigDecimal bd = new BigDecimal(str);
bd = bd.round(new MathContext(2, RoundingMode.HALF_UP));
System.out.println(bd.toPlainString());
This prints: 0.000000062
If you want to round down to 0.000000061 then use RoundingMode.DOWN
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('.'));
i have a Big Decimal like this for example.
i want to negative it's value. it has a string.
String a = "65";
BigDecimal example = new BigDecimal(a);
//i want to have (-65)
BigDecimal.negate()
String a = "65";
BigDecimal example = new BigDecimal(a);
System.out.println( example.negate() ); // prints -65
I used BigDecimal for rounding money value. And I have question.
float price = 0.71F;
BigDecimal priceA = BigDecimal.valueOf(price).setScale(2, RoundingMode.FLOOR);
//priceA == 0.70;
float price = 8.71F;
BigDecimal priceB = BigDecimal.valueOf(price).setScale(2, RoundingMode.FLOOR);
//priceB == 8.71;
Why ? And how rounding write ?
Never construct BigDecimals from floats or doubles. Construct them from ints or strings. floats and doubles loose precision.
This code works as expected. I just changed the type from float to String:
public static void main(String[] args) {
String doubleVal = "1.745";
String doubleVal1 = "0.745";
BigDecimal bdTest = new BigDecimal( doubleVal);
BigDecimal bdTest1 = new BigDecimal( doubleVal1 );
bdTest = bdTest.setScale(2, BigDecimal.ROUND_HALF_UP);
bdTest1 = bdTest1.setScale(2, BigDecimal.ROUND_HALF_UP);
System.out.println("bdTest:"+bdTest); //1.75
System.out.println("bdTest1:"+bdTest1);//0.75, no problem
}
Is there an easy formatter to format my String as a price?
So my string is: 300000 i'd like to "300 000" with space
or 1000000 "1 000 000"
Leslie
This does it:
String s = (String.format("%,d", 1000000)).replace(',', ' ');
Use Formatter class to format string
format("%,d", 1024);
After that replace , with space.
http://developer.android.com/reference/java/util/Formatter.html
You cannot do this with a simple format string but with the DecimalFormat and DecimalFormatSymbols class.
int value = 123456789;
DecimalFormat fmt = new DecimalFormat();
DecimalFormatSymbols fmts = new DecimalFormatSymbols();
fmts.setGroupingSeparator(' ');
fmt.setGroupingSize(3);
fmt.setGroupingUsed(true);
fmt.setDecimalFormatSymbols(fmts);
TextView txt = (TextView) findViewById(R.id.txt);
txt.setText(fmt.format(value));
There are lots and lots of other options in these classes. For example you could seperate the numbers with dots or commas or use a locale specific setting.
For example you can use the fmt.setCurrency method:
fmt.setCurrency(Currency.getInstance(Locale.GERMANY));
double yourPrice = 1999999.99;
String formattedPrice = new DecimalFormat("##,##0.00€").format(yourPrice);
output :
formattedPrice = 1.999.99,99€
if you have integer value, most elegant in my opinion
public static String formatNumberWithSpaces(long number) {
DecimalFormat formatter = (DecimalFormat) NumberFormat.getInstance();
DecimalFormatSymbols symbols = formatter.getDecimalFormatSymbols();
symbols.setGroupingSeparator(' ');
return formatter.format(number);
}