Solved

Control the digits after decimal point in float numbers

Posted on 2009-04-06
11
1,808 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
 
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
3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

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…
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

863 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

Need Help in Real-Time?

Connect with top rated Experts

25 Experts available now in Live!

Get 1:1 Help Now