How To Add Decimal Format Up To Two Places

Jul 25, 2014

import javax.swing.JOptionPane;
import java.text.DecimalFormat;
public class Sample3 {
public static void main(String args[]){
double amount,iRate,monPay,totalPay;
int years;
String amountStr;

[Code] ....

View Replies


ADVERTISEMENT

How To Format Methods To Display Number With 1-2 Decimal Places

May 8, 2014

I am working with a program I wrote for class. I got it to compile and do what I want, But I was wondering how I can format my methods to to display a decimal with 1-2 decimal places. Would I create a method in my NumberAnalysis class to do it for me? Or would I declare an instance of the DecimalFormat class in my main method?

import java.util.Scanner; //Needed for Scanner Class
import java.io.*; //Need for File and IOException
import java.text.DecimalFormat;
public class Ex8_11 { 
public static void main(String[] args) throws IOException{
{
DecimalFormat decformatter = new DecimalFormat("#0.00");
 
[Code] .....

This is my output:

Lowest Number: 1.09
Highest Number: 82.76
Total Number: 367.89000000000004
Total Average Number: 30.657500000000002

Think I just solved the answer to my own question, I did it by declaring double variables in my main method and called the methods and instantiated the variables into the methods... Is there a better way to do this?

package lesson4.skowronek;
 import java.util.Scanner; //Needed for Scanner Class
import java.io.*; //Need for File and IOException
import java.text.DecimalFormat;
public class Ex8_11 {
public static void main(String[] args) throws IOException{
 
[Code] ....

View Replies View Related

Formatting Answers To 2 Decimal Places

Sep 8, 2014

I've been scanning forums for answers to this problem, but most deal with simple programming that you might find in a classroom (i.e. "System.out.printf") which will not work in the GUI I'm attempting to complete. Here's the tale of the tape:

The GUI is a price calculator I'm developing for my company that takes input from drop-down menus and several Jtextfields and calculates the answer based on the values contained within each. It's completely done (and functional), so I'd rather not change too much if at all possible. Because I'm dealing with decimal values then I'm getting 9 decimal places in the output JLabel, though. In order to display the answer, I'm using a series of "totalPrice.setText(..." declarations.

Because there is a fair amount of text and the values in the calculation are constantly changing, is there a way to 'simply' format the output JLabel to display only 2 decimals? Or is there an alternative solution that would work--say with a JTextfield instead--without having to completely re-code the calculator?

View Replies View Related

Formatting User Answer To Two Decimal Places

Nov 20, 2014

I am writing a simple learning program that does basic math. Everything is working as needed except for when it comes to division. I need to know if there is a way to format the users input to two decimal places and that when the program checks the users answer against the division, it gives it a yay or nay. Right now, it wants the answer out to 12 decimal places.

So for 5/3

I would like it to accept the answer 1.67

not 1.666666666667

Is there a way to do this?

View Replies View Related

Printing 2 Decimal Places - Printf Not Working?

Mar 22, 2014

I need to print to 2 decimal places and I'm trying to use the printf method, but for some reason I keep getting an error. I've looked through my code, but unless I'm missing a small detail, it looks okay to me.

float sum = 0;
float avg = 0;
double[] rain = {0, 1.9, 1.5, 1.2, 1.1, 0.5, 0.03, 1.0};
 
//Calculate sum of rain.
for (int i = 1; i <= 7; i++) {
sum += rain[i];
 
[Code] ....

View Replies View Related

Formatting Doubles To Two Decimal Places Within Return Statement

Jan 25, 2015

I'm attempting to format my doubles to two decimal places within my return statement. I have tried to use DecimalFormat but it gives me an error because my method needs to return a double and that results in a string being returned. I have also tried using the *100.00/100.00 method and that doesn't work when the number already ends in 0.

If I pass -150.00 it gives me -150.0 when I need two decimal places.

How can I go about doing this?

View Replies View Related

Floating Point Precision - Output With Two Decimal Places?

Jan 27, 2015

Consider this small program and output.

public class Floating{
public static void main( String[] args ){
System.out.println( 2.80 - 1.75 );
}
}

The output is 1.0499999999999998. Why not 1.05?

View Replies View Related

Include Three Sides Of Triangle Formatted To Two Decimal Places

Sep 9, 2014

My program is supposed to include The three sides of the triangle formatted to two decimal places The perimeter formatted to one decimal place The area formatted to one decimal place The unformatted area. It does run, but it is not decimal formatted. I have read my book to try and figure out how to do this, but it doesn't make since to me.

import javax.swing.JOptionPane;
import java.util.*;
import java.text.DecimalFormat;
import java.util.StringTokenizer;
 
public class ShelbyHarms_3_03 {
public static void main (String [] args) {
double a, b, c; //Input sides of triangle

[Code] ....

View Replies View Related

Type Casting - Getting Number With Two Decimal Places As Output?

Jun 7, 2013

"Type Casting" .... So the question is "How am I getting a number with two decimal places as output?"

import java.util.Scanner;
public class SalesTax
{
public static void main(String[] args) {
double purchase, tax;
Scanner input = new Scanner(System.in);
System.out.print("Enter purchase amount: ");
purchase = input.nextDouble();
tax = purchase * 0.06;
System.out.println("Sales Tax: $" + (int)(tax * 100) / 100.0); +//this will give two decimal places??+
}
}

View Replies View Related

Convert Java Code To Display Using Printf Statement With Two Decimal Places To Right

Feb 26, 2015

How i would convert this java code to display using the printf statement, with two decimal places to the right...here is the source code so far, but it has a few errors and needs to be reformated for printf

import java.util.Scanner; // scanner class
public class PROB3_CHAL15
{
public static void main(String[] args)
{
double checks =0,
totalfee =0,
fee = 10,
fee1 =.1,
fee2 = .08,
fee3 = .06,
fee4 = .04,
checkFee;
String input;
Scanner keyboard = new Scanner(System.in);
 
[code]...

View Replies View Related

Program Cannot See DECIMAL FORMAT

May 12, 2015

I am using NetBeans IDE 8.0.2 to program Java code. I am a beginner in Java.

Instead of getting two decimal places after the point, I am getting three despite using the code format for two ("##.##"). Actually, this happens even if I remove the format code between the quotes. It is as if the program cannot see the format code. Why this happens ?

Here is the relevant program code:

private void convertButtonActionPerformed(java.awt.event.Action Event evt) {
double inputNumber = 0;
// sets the decimal format

[Code]....

View Replies View Related

Print Out GPA In Decimal Format

Oct 25, 2014

I need this program to print out the gpa down to 2 decimal places and I can't figure out how to do it. It keeps saying it can't find decimal format and I'm not sure how to define it.

Java Code:

import java.util.Scanner;
public class GPACalculator {
public static void main(String[] args) {
double creditHours = 0;
double gradePoints = 0;

[Code] ....

View Replies View Related

Program To Print In Decimal Format

Oct 24, 2014

so I need my program to print in decimal format and I keep getting an error saying that it cant find symbol "decimalFormat". here's what I have so far.

import java.util.Scanner;
public class GPACalculator {
public static void main(String[] args) {
int creditHours = 0;
int gradePoints = 0;

[Code] .....

View Replies View Related

Decimal Format Utility Of Java

Oct 13, 2014

I am working on a program where i calculate a fee using a method. I know i have to import the decimal format utility of java. "import java.text.DecimalFormat;" then i have to create an object for the decimal format. With "DecimalFormat f = new DecimalFormat("0.00");" What i am wondering how do i apply it to my system.out.println statement. I know i have to use for instance f.format(calculateFee()); Is that the right syntax for displaying the results because i generate a syntax error that way.

View Replies View Related

Decimal Format Errors - String Cannot Be Converted To Double

Jun 19, 2014

I am working on a project that just calculates simple interest based upon a principal and a percent interest rate written as .xxxx from user.

I need the Loan amount and the final interest calculation to show up as a currency with commas in appropriate areas, the Rate to be expressed as X.xxx% instead of .xxxx

I keep getting this error when I compile:

C:JavaInterestCalculator.java:46: error: incompatible types: String cannot be converted to double
principal = formatter.format(principal);
^
C:JavaInterestCalculator.java:49: error: incompatible types: String cannot be converted to double
rate = formatter.format(rate);
^
C:JavaInterestCalculator.java:52: error: incompatible types: String cannot be converted to double
totalInterest = formatter.format(totalInterest);
^
3 errors

Tool completed with exit code 1

And here is my code

import java.util.Scanner;
import java.text.NumberFormat;
import java.text.DecimalFormat;
// class declaration
public class InterestCalculator
{
// main method declaration

[Code] .....

View Replies View Related

Calculate Total Price At The End Of Order - Decimal Format

Apr 13, 2015

I have an assignment to create a JFrame pizzaorder and at the end of this order after the totalprice is calculated I need to format the number to ##.## but I keep getting a cannot find symbol error.

Source Code:

import java.awt.*;
import javax.swing.*;
import javax.swing.event.*;
import java.awt.event.*;
public class pizzaorder extends JFrame implements ItemListener, ListSelectionListener
//top section will have centered title telling the name, and under it seperate instructions to choose your pizza and toppings

[Code] ....

View Replies View Related

Not Getting Decimal For Surely Decimal Answer?

Jan 1, 2015

I'm using eclipse. I'm going to get straight to the point and give all the info I can, if the values in the first code box are used, shouldn't these values be left after all in the second box is done:

remainder=23, arr[0]=100, div=23/10=2.3, whole=2, and decimal=3?

When I use this code, div comes out to be just (2.0).

Java Code:

int leng=10;
arr[0]=123; //int
arr[1]=100; //int mh_sh_highlight_all('java'); Java Code: if (arr[0]!=arr[1]){
int remainder=arr[0]-arr[1];
arr[0]=arr[0]-remainder;
double div=remainder/leng; //double div=Double.valueOf(remainder/leng);
int whole=(int) Math.floor(div);
int decimal=(int) ((div-whole)*leng); mh_sh_highlight_all('java');

I'm not sure were I'm going wrong in how div is being calculated, but I ultimately need div to be 2.3.

I've also used the second option commented out which still gives (2.0).

View Replies View Related

Round Decimal Number Into 2 Decimal

Apr 2, 2014

Here is my code and i want to convert number into 2 decimal but this code not give me result how to check my code.

import javax.swing.JOptionPane;
public class showtime{
public static void main(String[] args){
double total_mili=System.currentTimeMillis();
JOptionPane.showMessageDialog(null,total_mili, "Total milisecond",JOptionPane.INFORMATION_MESSAGE);
double seconds=total_mili/60;
double sec=(double)(seconds * 100) / 100.000;
JOptionPane.showMessageDialog(null,seconds, "Total Second",JOptionPane.INFORMATION_MESSAGE);
JOptionPane.showMessageDialog(null,sec, "Total Second",JOptionPane.ERROR_MESSAGE);
}
}

View Replies View Related

Inserting Else Statement And Multiple Brackets At Various Places

Jan 16, 2014

I'm using this try statement to check for errors in input in my Tic Tac Toe game. The problem I have having is I don't understand why this error is happening. It's preventing my code from compiling. I've tried inserting an else statement and multiple brackets at various places with no success.

Java Code:
public void play(){
Scanner input = new Scanner(System.in);
int row, col;
char currPlayer = 'X';

[code]....

View Replies View Related

Monthly Temperature Chart Of Two Places - Array / While Loops

May 29, 2014

I started taking a java programming class javascript eclipse The program says it wants a monthly temperature chart of two places..Declare an array of values for Blueville temperatures and another array for Orlando temperatures. Then, use what you have learned to produce a program to output the following table:

Blueville Monthly temperatures

jan feb mar apr may jun jul aug sep oct nov dec

3 3 5 10 16 20 24 23 16 10 5 3

Orlando Monthly Temperatures

jan feb mar apr may jun jul aug sep oct nov dec

14 16 12 23 24 25 27 30 25 22 17 15

The warmest month in Blueville is _______

The warmest month in Orlando is ________

The month with the greatest temperature spread is __________

View Replies View Related

Sudoku - When Place Numbers More Than 4 At Random Places It Stops Responding

Apr 22, 2015

The thing my coding for sudoku is not working for few inputs... it works fine with all its value initially at 0, but when i place numbers more than 4 at random places it stops responding (it doesn't show any value).

My assignment is to get a solved sudoku for these values:

//Sample Input:
{0,2,7,3,8,0,0,1,0},
{0,1,0,0,0,6,7,3,5},
{0,0,0,0,0,0,0,2,9},
{3,0,5,6,9,2,0,8,0},
{0,0,0,0,0,0,0,0,0},
{0,6,0,1,7,4,5,0,3},
{6,4,0,0,0,0,0,0,0},
{9,5,1,8,0,0,0,7,0},
{0,8,0,0,6,5,3,4,0}

My Current code

public class Sudoku {
static int userGrid[][]=new int[][]
{{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0},
{0,0,0,0,0,0,0,0,0}};//[horizontal][vertical]
static int grid[][]=new int[9][9];//the grid that the program experiments on
public static void main(String[] args) {

[Code] ....

View Replies View Related

Decimal To Hex - Not Getting Value 0 When Input Is 0

Mar 4, 2014

Why do i not get a value 0 when my input is 0.

import java.util.Scanner;
public class Dec2Hex {
public static void main(String[] args){
int dec;
String hexStr = "";
int radix = 16;

[Code] .....

View Replies View Related

Add A Zero For Decimal Part

Apr 19, 2015

Add the following method to the BankAccount class: public String toString()Your method should return a string that contains the account's name and balance separated by a comma and space. For example, if an account object named benben has the name "Benson" and a balance of 17.25, the call of benben.toString() should return:

Benson, $17.25

There are some special cases you should handle. If the balance is negative, put the - sign before the dollar sign. Also, always display the cents as a two-digit number. For example, if the same object had a balance of -17.5, your method should return:

Benson, -$17.50

Here is my code:

public String toString() {
String result = name + ", ";
if (balance < 0) {
result += "-";
}
return result += "$" + Math.abs(balance);
}

My code only works in case there are full two numbers for the cents part, not for the case when there's only one number. So I wonder how I can add an extra zero when needed.I can get only the decimal part and add a zero if it's less than 10, but I don't know how I can extract just the decimal part from the number. (The balance is just a double and it doesn't have any separate field for dollars and cents).

View Replies View Related

Binary To Decimal Using Array

Jan 28, 2015

I do not know how to convert a reversed array to decimal.

The output should be:

How many digits to convert?
(user will input) ex. 3
Please input digits:
(user will input 3 digits)
ex. 0
1
1
The binary digits are: 110 (<-reversed)
The decimal value is: 6

Here is my code:

package numbersystemconversion;
import java.util.Scanner;
public class BinarytoDecimal {
static {
int digit=0;
Scanner input = new Scanner(System.in);

[Code] ....

View Replies View Related

Numbers After Decimal Won't Add Together For Total

Sep 26, 2014

The code is below and the problem I am having is the numbers after the decimal won't add together for the total I am teaching myself java through liang's 10th edition book and I took one of the sample programs further than what was shown. The one in the book only shows the tax amount I simply wanted to make it so that the purchase amount and the tax amount would add together

package javalearning;
import java.util.Scanner;
public class FindingTax {
public static void main(String[] args){
Scanner input = new Scanner(System.in);
System.out.print("Enter Purchase amount: ");

[code]....

View Replies View Related

Display Decimal Numbers Only

Jul 6, 2014

What code to use to display decimal numbers only. for example 125.50. the program will only display .50

View Replies View Related







Copyrights 2005-15 www.BigResource.com, All rights reserved