Solved

Control the digits after decimal point in float numbers

Posted on 2009-04-06
11
1,810 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
  • 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
JUnit 4 @Before and @BeforeClass differences 3 59
Java Timer (static) 9 43
xampp tool 12 48
hibernate example for saving data 19 37
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

776 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