Search:

I have a Double variable in my program that I just want to show it in this way: 1.123 , I mean

Asked By: Colleen    Date: Jan 16    Category: Java    Views: 1034

1) I have a Double variable in my program that I just
want to show it in this way:
1.123 , I mean I wanna to round it and just keep three
digit after .,
what is its method?
2) I used the ID as an auto number in SQL Server and
after using it a lot, it doesnâ€™t start
from 1, how can I again set it to start from 1.

Share:

Using the following method you do precesion upto any value.

/**
* This method provides a more sophisticated use of Math.round(double)
* for getting the the precision values.
*
* @param value The value to round.
* @param precision The basically an indicator of what digit to apply round
to.
* @return The resultant value.
*/
public static final float getPrecisionRound (float value, int precision) {
String sPattern = "####.0";
for (int i=1;i<precision ; i++)
sPattern += "0";
java.text.DecimalFormat df = new java.text.DecimalFormat (sPattern);
return Float.parseFloat (df.format(value));
}

So you should call this method as ---> getPrecisionRound (1.12345,3)
Here
1.12345 => The number you want to do precision.
3 => round it and just keep three digit after " ."

1) Use DecimalFormat
java.sun.com/.../DecimalFormat.html

2) no idea

Double d = 1.12323423424;
convert = new java.math.BigDecimal(d);
yValue =
convert.setScale(3,java.math.BigDecimal.ROUND_HALF_DOWN).doubleValue();

The value of yValue will be 1.123.

Why don't you just use a hash table and make the empid
your key? That would probably be the easiest way.

check for the parsers in jdk.. you ll know the rest..

in your JSP import org.w3c.dom library which is used to create the XML using DOM

If you want to output xml through jsp, you should specify the
contentType as text/xml

hope that will solve your problem.

I can give you the solution of the first problem.

The following method will help you to do precision upto any digits--

float getPrecisionRound (float value, int precision)
{
String sPattern = "####.0";
for (int i=1;i<precision ; i++)
sPattern += "0";
java.text.DecimalFormat df
= new java.text.DecimalFormat(sPattern);

return Float.parseFloat (df.format(value));
}

So u call this method as --> getPrecisionRound (1.23456,3)
Output ==> 1.234

for your first question, use the following.

If you want to round just use

> DecimalFormat:
> import java.text.DecimalFormat;
> public class Rounding {
> public static void main(String[] args) {
> DecimalFormat df = new DecimalFormat("#.00");
double  d = 9.99999999999;
> System.out.println(df.format(d));
> }
> }

Seems like the max-value on ur sequence generator is greater than the value it
is generating now. If you want it to wrap around, you have to alter it, set the
max value to the value of your choice and set the 'CYCLE' option. When the
sequence generator hits the max value it will start from the minimum value
specified in its definition.

A quick search in google gave me this.......Look into it
www.postgresql.org/.../sql-altersequence.html

Also note that this is better done at DB level.

In some of the other replies to your question people used
DecimalFormat. That is ok as long as the rounding method that you
want is ROUND_HALF_EVEN. Peronally I would use java.math.BigDecimal
to do number rounding. That way you can choose any rounding method
that you want. ie....

double d = 1.12345;
BigDecimal bd = new BigDecimal(d).setScale(3, BigDecimal.ROUND_HALF_UP);

If you want the string value:

String s = bd.toString();

If you want the double  value again:

d = bd.doubleValue();

As far as the SQL question is concerned I think if you just drop the
table and create again then the auto increment number will start at 1
again. You can also just "truncate" the table which in effect drops
and creates the table in 1 step.

1. mathematicaly yours. :)

public static double  format(double value,double ratio){
return
((double)(((long)(value*Math.pow(10,ratio))-(((long)value)*Math.pow(10,ratio)
))/(Math.pow(10,ratio))))+(long)value;
}

2. i love oracle