• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Devaka Cooray
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Carey Brown
  • Tim Holloway
Bartenders:
  • Martijn Verburg
  • Frits Walraven
  • Himai Minh

Round off a value

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

How do we round off a value to 6 decimal places and display it in a jsp?

Seb
 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi seb,

Get the value in to one float or double depand upon the value. import the util.* in the jsp page and use the desired function to round of the value .

Float f ;
f= 12.546465469546f;
Know the f value using the round function in java.util pack
i think this can help you.
 
Sheriff
Posts: 67682
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Not really a JSP question so moving along to the Java Intermediate forum.
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Christie George:
Hi

How do we round off a value to 6 decimal places and display it in a jsp?

Seb


Since you are doing this for display purposes, you should probably use java.text.NumberFormat. This class has methods to specify the number of digits before and after the decimal point. See the above link to the appropriate Javadocs for details.

Layne
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
There is a little trick - you multiply by 10^n where n is the number of decimal points you want, add 0.5, take integer, and divide result by 10^n. E.g. try this,

public class Rounder {
public static void main(String[] args) {
double n = 6; //no. of decimal places
double x = 123.12345678901234; //no. to be rounded
double y = (int)(x*Math.pow(10,n)+0.5)/Math.pow(10,n);
System.out.println(y); //result
}
}

Hope this helps,
Alastair
 
Ranch Hand
Posts: 1646
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Alastair Calderwood:
There is a little trick...

Since float and double values are not stored in decimal notation, they are only approximations of decimal values. While this trick will get you close, it won't be exact.

If you just want to round for display, use NumberFormat Layne said. If you need to use the rounded number in an exact calculation, you should use java.math.BigDecimal. The latter represents an exact decimal value to a very high number of decimal places and precision.
 
Well don't expect me to do the dishes! This ad has been cleaned for your convenience:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic