?
Solved

Math functions

Posted on 2003-02-24
9
Medium Priority
?
166 Views
Last Modified: 2013-12-29
hai,

I have a calculated double value for ex. 0.15682534735275272
but i want to display only 0.15,  only 2 or 3 decimal points.


can i do it,if so how?

0
Comment
Question by:rajuvegesna
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 8007930
String d = new DecimalFormat("0.00").format(0.15682534735275272);
0
 
LVL 1

Accepted Solution

by:
shji1 earned 400 total points
ID: 8008041
Simple:
double d = 0.15682534735275272;
double trimD = ((int)(d * 100)) / 100.0;

This will output 0.15, and I think it's faster then wrking with Strings.
Of course, you can create a generic method that will trim your value to any decimal location you like.
--Shaji
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8008086
Or do it the right way like CEHJ said ;-) *giggle*
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 1

Author Comment

by:rajuvegesna
ID: 8008091
good,but i couldnt able to keep in my prog..i am getting error,
because i want this string to be in the middle of a class file,like

String d = new DecimalFormat("0.00").format(p.y);

where p.y varies ...

what is the DecimalFormat actually....
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8008100
Soz....

looked a bit closer at your code, and you are right :-/

That will perform rounding to a certain number of d.p. and is quite cool :-)

*grovel* *apologise* :-(
0
 
LVL 35

Expert Comment

by:TimYates
ID: 8008102
So "String d = new DecimalFormat("0.00").format(p.y);" doesn't work?

what's the error?
0
 
LVL 1

Author Comment

by:rajuvegesna
ID: 8008129
good,but i couldnt able to keep in my prog..i am getting error,
because i want this string to be in the middle of a class file,like

String d = new DecimalFormat("0.00").format(p.y);

where p.y varies ...

what is the DecimalFormat actually....
0
 
LVL 1

Author Comment

by:rajuvegesna
ID: 8008145
hai Shaji ,

ur idea is good and simple..

thank u for all...
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 8008160
String d = new java.text.DecimalFormat("0.00").format(p.y);

Why would you want a number that is nearer to 0.16 to be displayed as 0.15?
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.
Suggested Courses
Course of the Month9 days, 6 hours left to enroll

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question