473,395 Members | 1,502 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Rounding a number (double) to specific decimal places

1
Hey guys I want a little help of yours. I am writing a program and I want to restrict its answer to a certain decimal places. (two decimal places) Her is my program,
Expand|Select|Wrap|Line Numbers
  1. /* To create a bank application capable of finding an interest */
  2. class bank
  3. {
  4.     public void enter ( double principal , double rate_of_interest , double time )
  5.     {
  6.         double SI = (principal * rate_of_interest * time) / (100.0);
  7.         double A = principal + SI;
  8.         double CItemp1 = (1+ (rate_of_interest/100.0));
  9.         double CItemp2 = Math.pow( CItemp1 , time);
  10.         double CItemp3 = principal * CItemp2;
  11.         double CI = CItemp3 - principal;
  12.         System.out.println("The amount obtained after calculating simple interest is " + A);
  13.         System.out.println("The Simple Interest for the required sum is Rs " + SI);
  14.         System.out.println("The amount obtained after calculating compound interest is " + CItemp3);
  15.         System.out.println("The Compound Interest for the required sum is Rs " + CI);
  16.  
  17.     }
  18. }
Mar 13 '14 #1
2 1564
Nepomuk
3,112 Expert 2GB
Hi surpsi and welcome to bytes.com!

I guess you're trying to format the output, correct? If so, take a look at the string formatting syntax; this will allow you to set various aspects of your output, including the precision of an output. And such formats are accepted by PrintStream#printf(...), which is exactly what System.out.printf(...) is.
Mar 13 '14 #2
chaarmann
785 Expert 512MB
If this is a bank application, you should never calculate with float or double, because you will get significant rounding problems. Just use BigDecimal class instead.

If the problem is very simple to solve (that means there is only adding, subtracting, and multiplying of whole numbers), then just convert the price to an integer and always calculate with that. Only during output, you would add the needed decimal point.
Example 3 Euro and 21 cent should not be stored as "double price=3.21", but as "int price=321", that means calculate always in cent instead of euro. At the end, if you want to output the Euros without the cents, don't divide by 100 because of rounding problems. Just convert the whole number to a string and cut off the last two characters.

By the way, just read about rounding problems, especially calculations with the number "0.10", which cannot be converted to an exact binary representation.
Mar 13 '14 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: Brent Bortnick | last post by:
Does anyone know how to find out the number of decimal places a number has. I need this info so that i can round to 3 decimal places if the number has 3 decimal places or to 2 decimal places if...
1
by: Gizmo | last post by:
Hi there i was wondering if any one new to a function that rounds up a float to so many decimal places. I have a number in bytes and converting it to mb's and gb's but once its converted i need to...
3
by: Dean G | last post by:
Hi, I need to truncate a number to two decimal places without rounding. All the functions i;ve tried tend to round up the numbers. Also i cant use any string functions to limit the size because the...
29
by: Gaijinco | last post by:
Sooner or later everytime I found recreational programming challenges I stumble with how I test if a number is has decimal places differnt than 0? For example if I want to know if a number is a...
3
by: Sandy | last post by:
Hello I need to have a value display in a label with two decimal places; e.g. "4" to display as "4.00 Any help will be appreciated Sandy
2
by: Andy | last post by:
Hi I'm really stuck outputting a double number to the console with three decimal places if the furthest right value is a zero. I can coutput the number 4.546 as 4.546 but then if I output...
3
by: paulquinlan100 | last post by:
Hi I have a couple of calculated fields in a report, i've set the Decimal Places property to 0 and the format to General Number, however when previewing the report it still comes up with a...
19
by: VK | last post by:
http://groups.google.com/group/comp.lang.javascript/browse_frm/thread/ b495b4898808fde0> is more than one month old - this may pose problem for posting over some news servers. This is why I'm...
1
by: jesmi | last post by:
hi!! i want to place two fix decimal places after a digit even if there is no decimal places after a digit. for example if there is a number 5, i want output 5.00 like that. please help me. ...
1
by: Serenityquinn15 | last post by:
Hi! I have a little trouble about in truncating numbers. What i want to do is to get the two decimal places. here's the example: when i try to get the half of 20500.39 it shows like...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.