how to negative bigdecimal's value in android - android

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

Related

How to get split string with special characters in android?

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('.'));

Remove digits which are in the decimal place

I get a double value (eg. -3.1234) which contains a value in decimals (0.1234 in this case). I want to remove the value in decimal value (in this case I wanted the value -3). How do I do it? I know how to remove the decimal if the decimal only has 0, but in this case I don't have the decimal value as 0.
Anyways here is the code for that:
DecimalFormat format = new DecimalFormat();
format.setDecimalSeparatorAlwaysShown(false);
Double asdf = 2.0;
Double asdf2 = 2.11;
Double asdf3 = 2000.11;
System.out.println( format.format(asdf) );
System.out.println( format.format(asdf2) );
System.out.println( format.format(asdf3) );
/*
prints-:
2
2.11
2000.11
*/
If you just want to print it :
String val = String.format("%.0f", 2.11)
// val == "2"
If you want to keep the double type but without decimals :
double val = Math.floor(2.11);
// val == 2.000
If you don't care about type you can cast the double into int :
int val = (int) 2.11;
// val == 2
//double as object
int val = myDouble.integerValue();
You can simply use the intValue() method of Double to get the integer part.
You can cast it to int
int i=(int)2.12
System.out.println(i); // Prints 2
Round up decimal value & convert it into integer;
Integer intValue = Integer.valueOf((int) Math.round(doubleValue)));
http://www.studytonight.com/java/type-casting-in-java check out type conversion in java
double asdf2 = 2.11;
int i = (int)asdf2;
System.out.println(asdf2);
System.out.println(i);
Output:
2.11`
2

Converting Exponential value to Decimal Android

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

Android BigDecimal how using write?

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
}

replace (,) in place of (.) in textview in android

I am using this code to add two number after (. in my number. For example: I have string 14.3, so I want to get 14.30, when get 14 I want to get 14.00. This is code:
NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
tvPrice.setText(addDolar(format.format(Double.parseDouble(alerts.getPrice()))));
private String addDolar(String amount) {
if(amount.startsWith("-")) {
return "-$ "+amount.substring(1, amount.length());
}
else
return "$ "+amount;
}
problem is that I want to get '.' and now i get ','.
You can replace it:
someDouble.toString().replace(",", "."))
If you want to add two precisions only, then try this code
DecimalFormat format = new DecimalFormat("##.##");
String formatted = format.format(your_value);
editText.setText(formatted);
Use this function :
public double round(double unrounded)
{
BigDecimal bd = new BigDecimal(unrounded);
BigDecimal rounded = bd.setScale(2, BigDecimal.ROUND_HALF_UP);
return rounded.doubleValue();
}
Try following
NumberFormat format = NumberFormat.getNumberInstance();
format.setMinimumFractionDigits(2);
format.setMaximumFractionDigits(2);
tvPrice.setText(addDolar(format.format(Double.parseDouble(alerts.getPrice()))));
private String addDolar(String amount)
{
amount = amount.replace ( ",","." ); // Add this line
if(amount.startsWith("-"))
{
return "-$ "+amount.substring(1, amount.length());
}
else
return "$ "+amount;
}

Categories

Resources