Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
First time using the DecimalFormat classes...
I need to store 4 decimals precision for my value.
Let me show you what I have done so far.
public class Paper
{
public String name, color, type,finish,grain;
public double width,height,gsm, lbs,ppi;
public DecimalFormat df;
public Paper()
{
String pattern = "#0.0000";
df = new DecimalFormat(pattern);
}
public void setWidthAndHeight(String width, String height)
{
//I've censured what I tried so far.
///I need to ensure that the passed width has 4 decimals and that it is stored that way.
this.width = //ENTER answer here! :)
}
Thank you for your time, and patience.
You can use BigDecimal to manage your decimals.
BigDecimal n = new BigDecimal("100.12345");
n = n.setScale(4, RoundingMode.HALF_UP);
n = n.stripTrailingZeros();
System.out.println(n.toPlainString());
Using scale will round your number to 4 decimal precision. Remove any zeros using stripTrailingZeros and use toPlainString to get your number later for printing.
http://docs.oracle.com/javase/7/docs/api/java/math/BigDecimal.html
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
My input field BigDecimal num may contain both integer and floating numbers.
num = 45
or
num = 45.5343434
If the BigDecimal input field num contains decimal places, then I would like to limit the decimal places to 4.
Desired Output : 45.5343 if input num = 45.5343434
Desired Output : 45 if input num=45
How can I do that?
You are interested in what is called the scale of your BigDecimal.
Call BigDecimal#scale.
BigDecimal x = new BigDecimal( "45.5343434" );
BigDecimal y = new BigDecimal( "45" );
x.scale(): 7
y.scale(): 0
You asked:
I would like to limit the decimal places by 4
Test for the scale being larger than four. If so, round.
BigDecimal num = new BigDecimal(45);
if (num.scale() > 4) {
num = num.setScale(4, RoundingMode.HALF_UP);
}
System.out.println(num);
Output:
45
In case of more decimals:
BigDecimal num = new BigDecimal("45.5343434");
45.5343
You can choose a different rounding mode to fit with your requirements.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
I have BMI calculator app that takes some input and puts it in "EditText". Here is what I am trying to do:
If the input is 170, it will become 1.70.
If the input is 1.70, it will not change.
This is the code I have:
String weight = editText.getText().toString();
Cant you convert the string to an integer and take modulus 100 to the cms and divide by 100 to get in meters?
You can convert the String weight to int like this
int wgt = Integer.parseInt(weight);
Then separating meters and centimetres.
int mtrs = wgt / 100;
int cms = wgt % 100;
Then combining both
String result = mtrs + "." + cms;
try something like this
float value = Float.parse("170");
editText.setText(String.Format(Locale.ENGLISH,"%,02f", value/100f))
I found this way works: I take the number that the user put, and then "170/100" + "0" gives me 1.70
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
So, I am making a basic tip calculator and I need to know how to change the users input of 15%, 23%, etc. into 0.15, 0.23, etc.
This is my current code:
import java.util.Scanner;
public class TipCalc {
public static void main(String args[]) {
Scanner meal = new Scanner(System.in);
double food, tax, tip, fin;
System.out.println("How much was the meal?");
food = meal.nextDouble();
System.out.println("How much was the tax?");
tax = meal.nextDouble();
System.out.println("How much would you like to tip? I recomend 15%");
tip = meal.nextDouble();
}
}
Please help! Thanks!
double tip;
tip = meal.nextDouble() / 100;
Or
double tip = 15.0;
tip = tip / 100;
Or Even
double tip = 15.0;
tip /= 100;
If the input for your tip is 15.
Using
tip = meal.nextDouble() / 100.0;
System.out.println(tip);
would OUTPUT: 0.15
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 9 years ago.
Improve this question
Is it possible to change the meaning of the asterisk symbol (multiplication operator) in Java?
I want to do this:
int ex = 600 * 0.98;
It fails because you can't convert from double to int. Can I make it so that I'm able to convert the double to an integer? Or does the
asterisk only have one meaning that can't be changed?
I just want the OPERATION in math, not a string.
To have an operation, you must have code. One way of doing this is to have a method like
public static double ex(double factor) {
return factor * 0.98;
}
or if the factor is a field
private double factor = 600;
public double ex() {
return factor * 98 / 100;
}
public static void main(String... ignored) {
Main m = new Main();
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
m.factor = 700;
System.out.println("factor: "+m.factor+" ex: "+ m.ex());
}
prints
factor: 600.0 ex: 588.0
factor: 700.0 ex: 686.0
As you can see ex() is re-evaluated each time it is used.
Why do I * 98 / 100? I do this as each value can be represented exactly however 0.98 is not represent exactly and can have a small error.
System.out.println(new BigDecimal(0.98));
prints the closest representable value to 0.98
0.979999999999999982236431605997495353221893310546875
If you want to store text you need to do something like
String ex = "600 * 0.98";
An int value is for storing a whole number which is a 32-bit signed value, nothing else.
Uhhh.
The short answer is that you can't do that. A variable of type int can only store an integer value. The expression you assign to the int has to evaluate to an integer.
What integer value do you want assigned to ex?
int ex = 588; // 600 * 0.98
What meaning are you associating with the asterisk between two numeric values that is not multiplication?
If you want to store an array of characters, then:
char[] ex = "600 * 0.98".toCharArray();
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I understand that you can set the decimal places being printed for a float by doing this %.2f but I want to print only significant figures:
1.33443
1.3
2.00006
Use this
double roundTwoDecimals(double d) {
DecimalFormat twoDForm = new DecimalFormat("#.##");
return Double.valueOf(twoDForm.format(d));
}
public static void main(String [] args) {
float f1 = 1.3344300f;
float f2 = 1.3000f;
float f3 = 2.010f;
System.out.println(f1);;
System.out.println(f2);;
System.out.println(f3);;
}
Always prints...
1.33443
1.3
2.01