# decimal « decimal « Java Data Type Q&A

### 1. Lightweight Java Decimal Class    stackoverflow.com

I am considering writing two limited precision alternatives to BigDecimal, namely DecimalInt and DecimalLong. These would be capable of dealing with numbers within the real bounds of int and long ...

### 2. truncating number after decimal...through built in function in java    stackoverflow.com

i want to truncate a number after decimal through a built in function in *java* (not javascript). for example: 69.80000000001 to 69.8 please guide.

### 3. find decimal in a given number    stackoverflow.com

How can i find decimal(dot) in a given number in java. I am getting input from user, he may give integer or float value. I need to find he entered integer or ...

### 4. Function to calculate with decimals in Java    stackoverflow.com

This code is works fine when you type 22 and 56 for example, it shows you the correct result

``````  import java.util.Scanner;

class apples{
public static void main(String ...``````

### 5. Fixed decimal numbers with JAVA    stackoverflow.com

How do I represent numbers in the format

``````001 = 1
002 = 2
003 = 3
010 = 10
020 = 20
``````
The number of digits is always ...

### 6. logic for checking sequential order in decimal numbers using java    stackoverflow.com

I want to check the sequential order of decimal numbers and find the missing number. For eg: If i have 1.1.1, 1.1.3, 1.1.4, 2.1.1, 2.1.3, 2.1.2, 3, etc Here i need to find ...

### 7. Best Java Object for Incredibly Small Decimals    stackoverflow.com

What java object is best suited to hold tiny decimal values, such as 10^-25? What object will maintain the value of the number most accurately, while using the least space? I simply ...

### 8. Unexpected decimal result    stackoverflow.com

I run the following code, but did not expect that result..

``````public class SampleDemo {
public static void main(String args[]) {
...``````

### 9. Storing large decimal numbers in JAva    stackoverflow.com

I need to store 17774132 in a double format, but it seems that double is to small since I get 1.7774132E7. How can I overcome this problem? I need some kind of ...

### 10. wrong java decimal and therefore wrong calculation    stackoverflow.com

The output I got is like this:

``````number of bags ordered : 43 (\$236.50)

discount : 5% (\$11.83)

total cost : \$224.68
``````
that correct total cost should be `224.67` because `236.50 - 11.83 = 224.67` the ...

### 12. BCD to decimal and viceversa    coderanch.com

Hi, Am working on conversion of decimal to BCD (Binary coded decimal) and viceversa. I require BCD for calling Cobol from Java using JNI. Can anybody help me out in giving a pointer or an algorithm for doing the same. I Googled on the web for any info on BCD conversion, but all in vain. Please help me out. TIA, Prashanth ...

### 13. a simple addition of decimal numbers    coderanch.com

In our project, we are taking care that all the float numbers shuld be of two decimal places only and we r not using something like BigDecimal for this to keep it simple. So, at all the divisions I am taking care of rounding it to two decimals. Now I found a strange test case, where in adding two numbers with ...

### 15. Validating a decimal number    coderanch.com

Hi, My requirement is to validate a decimal number. I have the information about the maximum integer digits and maximum allowed digits in the fraction part. What can be the best way to do it? I am unable to use DecimalFormat... Is there any way i can make use of set the interger and fraction part using setMaximumIntegerDigits and setMaximumFractionDigits of ...

### 16. Decimal Convertion    coderanch.com

Hi I need to print the Double Value with 2 decimal digits. i am getting the double value like 1.07142858E7,but actually i need to show the value like this 10714285.8(this is the converted value) Can any one suggest me that how can i print the converted value. (1.07142858E7 as 10714285.8) Thank you Naresh

### 17. Finding recurring cycle in a decimal.    coderanch.com

What I can tell you however is that a recurring fraction a/b will recur in less than b places. Hmm.. didn't know that, thanks. Isn't it also true that a rational number will either be a terminating or a recurring decimal? that is, it will never be both non-terminating and non-recurring?? If so, using BigDecimal with an appropriate scale, ...

### 19. Obtaining only a decimal portion of a number.    coderanch.com

It's been quite a while since I used JAVA, I'm finishing undeprecating alot of the code I wrote years ago (Can it be that long?) I need a method that will only return the decimal portion of a number (i.e. fx(123.456) would return .456) but I can't find a built in method to do so. I know other languages have this ...

### 20. how to subtract decimal????    coderanch.com

hi guys!!! i had a program that subtract two double variable.here's my code: import java.awt.*; import java.awt.event.*; public class Try { public static void main(String argv[]) { Frame fr = new Frame(); fr.pack(); fr.setVisible(true); Panel panel = new Panel(); final TextField tf1 = new TextField(15); final TextField tf2 = new TextField(15); final Button subtract = new Button("Minus"); Button clear = new ...

### 21. Octals, Decimals and Hexadecimals    coderanch.com

I think that he means by hand. Octal uses a ones place, and 8's place, a 64's place, 512 etc. Basically you just need to find out how many of each that you need. To convert 489 decimal to octal 512's = 0 too big 64's = 7 divide 489 by 64 = 7 remainder 41 8's = 5 divide 41 ...

### 25. Decimal Powers.. simple question    coderanch.com

Mixed-type arithmetic is tricky. Doing math with some integers and some doubles means that some of your partial results ( that "aNote/440", for example) are going to be done in integer math, which of course truncates fractional parts. The quickest fix here would be to use doubles for all the constants: public double getFrequency(int midiNote) { double aNote = 440; return ...

### 26. Binary/Octal/Hex and Decimal Number Systems    coderanch.com

the different number systems simply refers to how many digits you can use. humans use 10 digits... 0-9. to count, we start cycling through them all. when we run out, we start using two digits at a time. when we run out of every possible 2-digit combination, we go to three. so a number like 243 really means 2*10^2 + 4*10^1 ...

### 28. getting the right decimal...    coderanch.com

I have been working with an online class and have this project to complete. I have everything working, Except the deicmals.. (ie. if I have \$478.00 I only get \$478.0 printed). What do I do to get the other zero?.. Below I have attached my code incase I've over looked something.. class payRoll { public static void main (String [] args) ...

### 30. Decimal Subtraction    coderanch.com

I am trying to subtract decimal numbers but I'm not getting the desired results. For instance when I subtract 12 from 12.92 I should get 0.92 instead I the answer is 0.9199999999999999999. What can I do to get 0.92? If I round it off I'll get 1 which is not the answer i want. Your help will be highly appreciated.

### 32. adding decimal numbers    coderanch.com

Fernando Dominguez wrote:Hello, I'm trying to add to Doble numbers with 2 decimal places, 12,25 + 23, 46 = 35, 71 I get most of the time. But some times, for example, 12,10 + 20,15 gets 32,24999999999999999999996 Why? Thanks in advance I'm a student engineer, no java expert at all but I can tell you a bit about numerical math: A ...

### 33. Decimals in Java    coderanch.com

I have a variable which was defined as a double(primitive data type).Now this variable is supposed to store 8 digits or more with decimals.For ex.778906211.35.But this is stored in an exponential format-example 7.7890621135E8.How do I store it so that the entire format is shown-ie as 778906211.35.Should I use the BigDecimal Class? Please advise. Thanks Richard

### 34. Getting rid of unwanted decimal numbers    coderanch.com

The problem asks for me to list side by side how many pounds are in a kilogram from 1 to 199. I have everything right except all these random zeros pop up after each of the pounds. For example, 3 kilograms = 6.600000000000005 pounds. I have read on this else where, and everyone suggests using the decimal format, DecimalFormat("#,###.##"); , but ...

### 35. How to get rid of unwanted decimal numbers    coderanch.com

The problem asks me to list numbers from 10,000 to 100,000 on the left side and the PI value for every 10,000. Everything is correct except for all of the decimal numbers that pop up. for example, 10000 multiplied by PI is 3.1415.8999999998. I have read on this else where, and everyone suggests using the decimal format, DecimalFormat("#,###.##"); , but unfortuantely ...

### 36. Decimals in java help!!    java-forums.org

hi i need some help with java ive got an assignment and 1 of the questions is 22 divided by 1000000 see i no the answer is 0.000022 but everytime i try it in bluej i keep gettin the answer as 0.0 ive tried float and double could anyone suggest another way for me to get my answer showing correctly thank ...

### 38. Decimal Fill up    java-forums.org

Hey guys Below mentioned is a simple program which i wrote that will decide how many digits to display after decimal place. class Exercise1 { public static void main(String[] args) { double d = 2344.5; System.out.println("%.5f", d); } } Now as per the above code the output should be 2344.500000, but instead of that i get an error cannot find symbol, ...

### 39. truncate a decimal number ?    java-forums.org

I have a double variable, value of it is 0.8251913258676211, But I want to display only 0.82 . And Later than I am going to use this 0.82 for arithmetic expression so I can not do toString(). Is there anyway to do this and how?. I am not trying to round my number; I got my number as a result of ...

### 40. Trying to store a decimal between 0&1 after dividing by 100    java-forums.org

Hi, I'm not totally new to Java, been playing for a while but I've hit an embarassing brick wall. The answer to this must be simple but I just can't get it!!! Basically I'm playing with making a little text based game. Each enemy has an amount of damage it can do, and I'm trying to make it relate to it's ...

### 41. using decimal numbers    java-forums.org

First off, I did search a bit. But being new and not really understanding much I'm not quite sure how to word things to get the answer I want. What I was able to find confused me more. But I did find a trend in answers, to which I'm sure someone will elude to.

### 43. Got an error when enter the number in decimal eg 30.23    java-forums.org

I am at the very initial stage of learning Java.I wrote basic program called account.Program is working fine when I enter the number without decimal eg.34,64....But it gives the following error when I enter the nuber with decimal eg.34.87,54.65 ************************************************** ******** account1 balance:\$50,00 account2 balance:\$0,00 Please enter the amount you want to deposit in account1 : 45.63 Exception in thread "main" ...

### 45. Beginner trying to write Java code, has issue w/ printing result and 2 decimals    java-forums.org

I'm taking a Java class which I am really liking so far. So I have this problem I am working on now. A. Create a class name purchase. Each purchase. Contains an invoice number, amount of sale, and amount of sales tax. Include set methods for the invoice number and sale amount. Within the set() method for the sale amount, calculate ...

### 46. Payroll program will not print decimals... could someone help?    java-forums.org

import java.util.Scanner; public class Payroll2 { public static void main( String[] args ){ Scanner input = new Scanner( System.in ); { System.out.print ( "Enter employee name: " ); String theName = input.nextLine(); { } int hours; { System.out.println("Enter hours worked:"); String line = input.nextLine(); hours = Integer.parseInt(line); if(hours < 0) System.out.println("number must be positive"); } while(hours < 0); int pay; do ...

### 47. Java does not read the decimals?    java-forums.org

Hello. I was going to post this on the Appleta' site, as it's a problem from an applet, but as I overloaded it with threads today and I think it's a general Java problem, I decided to post it here. Well, the thing is that I've got a varyable named "x", and I set it at 1. Summarizing it all, t ...

### 48. Need help with truncating decimals?    java-forums.org

import java.util.Scanner; public class Args{ public static void main(String[] args) { Scanner in = new Scanner(System.in); double iBalance; int month=0; int year=0; int finalBalance; System.out.println ("Enter your Initial Balance."); iBalance=in.nextInt(); in.close(); System.out.println("Balance is:\n"+iBalance); while (iBalance > 500) { iBalance = iBalance*(1.005)-500; month++; } while (month>=12) { year = month/12; month = month-12; } if (year > 0) { System.out.println (+year+" years ...

### 49. Help with decimals    java-forums.org

Hi I am trying to write a program which works out a bunch of stuff, and I want the final answer to be given to 2 decimal places Here is an excerpt from the code, the bit which I need to be given to 2 decimal places is the last two lines. I used 2 classes, om is a copy of ...

### 50. Getting/changing the decimal symbol?    java-forums.org

I have a string containing a decimal number. But if I convert it to a float or double, the decimal symbol will either be be ignored or the method throw a NumberFormatException (I'm not sure which one) if the program is run on a machine with a different decimal symbol specified in the settings than what is used in the string, ...

### 51. parseInt with decimals    forums.oracle.com

I'm in the second week of my Java class, so try to keep it simple for me! I'm trying to change the decimal entry someone inputted into my program by entry = Integer.parseInt(response); obviously, this doesnt work because it changes the decimal to a whole number... which I can't have for this specific program. What would be the command I would ...

### 54. Adding decimals - help!    forums.oracle.com

Can Java add 10.2 + 0.1 and output the 10.3? I tried all sorts of things, and I can't figure out how! Here is one test: double d = 10.2; double delta = 0.1; double c = d + delta; System.out.println(c); The output is 10.299999999999999 It is really important that I get an exact result, because I need to increment numbers ...

### 56. Newbie Needs help with decimals    forums.oracle.com

class ArrayTest { public static void main( String [] args) { double[] anArray1; double[] anArray2; anArray1 = new double[3]; anArray2 = new double[3]; anArray1[0] = 7; // product number anArray1[1] = 25; // amount in stock anArray1[2] = 75.99; // price per unit anArray2[0] = 13; // product number anArray2[1] = 12; // amount in stock anArray2[2] = 59.99; // price ...

### 57. Help with project: trying to create variables from decimals    forums.oracle.com

topic may be a bit unclear, but I have a project from a java course. I need to create a simple program, but hit a bit of a bump. I have made a formula to find that the total hours equals 2.7350 which is equal to 2hours, 44minutes, 6seconds. so to sum what I just said up, I have the number ...

### 59. need help with decimals    forums.oracle.com

not the greatest of examples, but thx for the references here's what i need help with public class pro24{ public static void main(String args[]) throws java.io.IOException { int days = 180; double dist = 2.5; //in miles: going one way int mpg = 26; // miles per gallon double gpg; //cost of gas per gallon System.out.println("How much does gas cost per ...

### 63. Please check out this : Roman to decimal    forums.oracle.com

Decimal/Roman Conversion Roman numerals consist of the letters I, V, X, L, C, D, M which represent the values 1, 5, 10, 50, 100, 500, 1000. These letters are combined in an order-value system. Basically the rule is, if a letter is followed by an equal or lower valued one it counts as positive. If it is followed by a higher ...

### 64. Calculating pi.. decimals?    forums.oracle.com

/*Calculation the value of pi: Calculate the value of pi from the infinite series pi=4-4/3+4/5-4/7+4/9-4/11-... Print in a table that shows the value of pi approximated by computing the first 200.000 terms of this series. How many terms do you have to use before you first get a value that begins with 3.14159? */

### 68. Decimal problem (while calculating PI)    forums.oracle.com

Here's an explanation of why your algorithm does not work: Imagine a (pizza) pie; if you cut it in two, you'll have two halves of it. You leave one alone and cut the other; again you have two halves. You can keep doing that for a long time and you'll always have something left to split in two, but it will ...

### 69. Recurring Decimal    forums.oracle.com

OK, 2 things here: 1 - I don't see anyplace that you actually set a decimal point. 2 - to my knowlege you cannot add an int to an ArrayList--when I try to do it, I've always gotten an error. Possibly there is an implicit conversion from int to your Integer type, if that is so, then see comment 1.

### 70. RowFilter Not working for decimals    forums.oracle.com

The problem is not with parsing of String. Number filter takes Number object (in this case Double) and uses it's equals method, which actually compares internal double value. That value is stored as a binary fraction. Not every decimal fraction can be exactly mapped to a finite binary fraction much less to one that fits completely into double's 64 bits. Thus ...

### 71. please up decimals?    forums.oracle.com

mjohnsonaz74 wrote: Never use a Double or a Float for price information. You lose precision. Always use a BigDecimal for prices. For currency, the BigDouble.toString() should be sufficient. Or you could use a number formatter for finer control. Yes, use BigDecimal if you want your code to be unreadable. A better solution is to use ints or longs and store your ...

### 74. dertermine if an excel cell contains a decimal - apache poi    forums.oracle.com

What's simple about it? If it's a number cell it just contains a number. And a number doesn't "have a .0", it's just a number. Perhaps you're asking about the formatting of the cell? That displays the number to the Excel user as a string. If that's the case, then get the string value of the cell or something like that. ...

### 76. equivalent for decimal type    forums.oracle.com

The binary representation of a Decimal value consists of a 1-bit sign, a 96-bit integer number, and a scaling factor used to divide the 96-bit integer and specify what portion of it is a decimal fraction. The scaling factor is implicitly the number 10, raised to an exponent ranging from 0 to 28.

### 77. Decimals in Calculator Code    forums.oracle.com

Are you asking for 10 / 2 display 5 but for 9 / 2 display 4.5? If so then you can use the mod operator. If x % y == 0 then do integer division otherwise do floating point division. Or there maybe some other uber mathematical way to do it that only math wiz's like Jos know about.

### 80. Packed Decimal Data validation    forums.oracle.com

Hi, For legacy system..we are getting Date and time in packed decimal format.I wanted to put validation for time . Time should be an integer value.. But whatever may be value of time (String alphabet or int),while converting from packed decimal to long,its doing successfully. but i wanted to put an check ,and allow only integer value. PackedDecimalConverter converter = new ...

### 81. Problems with decimal.    forums.oracle.com

Thanks again for responding. Fully understand the issue of monetary values. I happen to BigDecimal to avoid going crazy. It will be a tedious job because all the software is developed with float and never could tell because he had problems with decimals. But I am happy to find the problem and know how to solve it. Thank you very much. ...