Logo 
Search:

Java Forum

Ask Question   UnAnswered
Home » Forum » Java       RSS Feeds

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: 1366
  

I have some questions, Would you please help me?
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: 

 

12 Answers Found

 
Answer #1    Answered By: Shelia Wells     Answered On: Jan 16

I can help you regarding the first problem.
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 " ."

 
Answer #2    Answered By: Roop Kapoor     Answered On: Jan 16

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

2) no idea

 
Answer #3    Answered By: Abasi Massri     Answered On: Jan 16

For your 1st question
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.

 
Answer #4    Answered By: Aylin Kaya     Answered On: Jan 16

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

 
Answer #5    Answered By: Rhys Evans     Answered On: Jan 16

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

 
Answer #6    Answered By: Mildred Bailey     Answered On: Jan 16

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

 
Answer #7    Answered By: Lee Butler     Answered On: Jan 16

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

hope that will solve your problem.

 
Answer #8    Answered By: Jennifer Davis     Answered On: Jan 16

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

 
Answer #9    Answered By: Beaudi Smith     Answered On: Jan 16

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));
> }
> }

 
Answer #10    Answered By: Sophia Campbell     Answered On: Jan 16

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.

 
Answer #11    Answered By: Andrew Brown     Answered On: Jan 16

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.

 
Answer #12    Answered By: Gustavo Costa     Answered On: Jan 16

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

 




Tagged: