Solved

Control the digits after decimal point in float numbers

Posted on 2009-04-06
11
1,827 Views
Last Modified: 2012-05-06
How I can control the digits after decimal point in float numbers ?

For example I have this number 11.656565 I want to display as 11.65

I need an example in JSP
0
Comment
Question by:bworld
[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
  • 2
  • 2
  • +1
11 Comments
 
LVL 26

Expert Comment

by:ksivananth
ID: 24075050
use BigDecimal!
0
 
LVL 26

Accepted Solution

by:
ksivananth earned 125 total points
ID: 24075056
for e.g.,

new BigDecimal( 11.656565 ).setScale( 2, BigDecimal.ROUND_HALF_UP ).doubleValue() ;
0
 

Author Comment

by:bworld
ID: 24075106
ksivananth

I used the previous statement with out.print but I get error as follow
out.print(new BigDecimal(fldValue).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue() + " %");



HTTP Status 500 - 
 
--------------------------------------------------------------------------------
 
type Exception report
 
message 
 
description The server encountered an internal error () that prevented it from fulfilling this request.
 
exception 
 
org.apache.jasper.JasperException: Unable to compile class for JSP
 
An error occurred at line: 156 in the jsp file: /murad_scripts/imfIdentifyDefault.jsp
Generated servlet error:
BigDecimal cannot be resolved to a type
 
An error occurred at line: 156 in the jsp file: /murad_scripts/imfIdentifyDefault.jsp
Generated servlet error:
BigDecimal.ROUND_HALF_UP cannot be resolved to a type
 
 
	org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	com.moximedia.aims.servlet.filter.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:58)
	net.geocortex.imf.security.http.AuthorizationFilter.doFilter(AuthorizationFilter.java:147)
 
 
root cause 
 
org.apache.jasper.JasperException: Unable to compile class for JSP
 
An error occurred at line: 156 in the jsp file: /murad_scripts/imfIdentifyDefault.jsp
Generated servlet error:
BigDecimal cannot be resolved to a type
 
An error occurred at line: 156 in the jsp file: /murad_scripts/imfIdentifyDefault.jsp
Generated servlet error:
BigDecimal.ROUND_HALF_UP cannot be resolved to a type
 
 
	org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
	org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
	org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
	org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
	org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
	org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:303)
	org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
	javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
	com.moximedia.aims.servlet.filter.ResponseHeaderFilter.doFilter(ResponseHeaderFilter.java:58)
	net.geocortex.imf.security.http.AuthorizationFilter.doFilter(AuthorizationFilter.java:147)
 
 
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.17 logs.
 
 
--------------------------------------------------------------------------------
 
Apache Tomcat/5.5.17

Open in new window

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 16

Assisted Solution

by:Peter Kwan
Peter Kwan earned 125 total points
ID: 24075362
Hi, bworld.

You need to import the BigDecimal class like this:

<%page import="java.math.BigDecimal" %>
0
 

Author Comment

by:bworld
ID: 24084889
How I use output.print with BigDecimal statement ?

How I can use BigDecimal statement to represent values of variables ?

I need an examples for that
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24123754
0
 

Author Comment

by:bworld
ID: 24129841
I need an example with JSP for that

For example if I have this variable
vr_Avg = 89.996527

I want to show like
89.99

How ?
0
 
LVL 92

Assisted Solution

by:objects
objects earned 250 total points
ID: 24133467
0

Featured Post

Interactive Way of Training for the AWS CSA Exam

An interactive way of learning that will help you visualize core concepts so that you can be more effective when taking your AWS certification exam.  Built for students by a student to help them understand the concepts that they are being taught.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Suggested Courses

636 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