Velocity Reviews > Java > float up to "4 decimal "

# float up to "4 decimal "

why
Guest
Posts: n/a

 04-03-2004
Hi,

My problem is about float variable .
suppose my java program gives(output)
X =-0.3059915

I want to take only upto four decimal means ,(to use
it as input )
X=-0.3059

How can I do that in java?

Thank You .

VisionSet
Guest
Posts: n/a

 04-03-2004

"why" <> wrote in message
news: om...
> Hi,
>
> My problem is about float variable .
> suppose my java program gives(output)
> X =-0.3059915
>
> I want to take only upto four decimal means ,(to use
> it as input )
> X=-0.3059
>
> How can I do that in java?

p = 4 // decimal places
float x1 = -0.3059915
float x2 = (int)(x1*10^p)/10^p // ^ = pseudo for Math.pow() (or whatever it
is)

--
Mike W

Roedy Green
Guest
Posts: n/a

 04-03-2004
On 3 Apr 2004 10:56:14 -0800, (why) wrote or
quoted :

>I want to take only upto four decimal means ,(to use
>it as input )
>X=-0.3059

See http://mindprod.com/converter.html
for how to convert a float to a String.

--
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.

Chris Smith
Guest
Posts: n/a

 04-07-2004
why wrote:
> Hi,
>
> My problem is about float variable .
> suppose my java program gives(output)
> X =-0.3059915
>
> I want to take only upto four decimal means ,(to use
> it as input )
> X=-0.3059

See java.text.DecimalFormat.

--
www.designacourse.com
The Easiest Way to Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation

Michiel Konstapel
Guest
Posts: n/a

 04-09-2004
> My problem is about float variable .
> suppose my java program gives(output)
> X =-0.3059915
>
> I want to take only upto four decimal means ,(to use
> it as input )
> X=-0.3059
>
> How can I do that in java?

See java.text.DecimalFormat.
HTH,
Michiel

mromarkhan@rogers.com
Guest
Posts: n/a

 04-09-2004

Maybe there is an easier way, but here goes

import java.text.DecimalFormat;
class FormatThis2{
public static void main(String[]args) {
float X =-0.3059915f;
DecimalFormat df = new DecimalFormat("0.0000");
System.out.println(df.format(X)); // - 0.3060
DecimalFormat dfloor = new DecimalFormat("0.00000");
float Y =-0.3059915f;
String result = dfloor.format(Y);
result = result.substring(0,result.length() - 1);
double dbl = Double.parseDouble(result);
System.out.println("heloo " + dbl);
}
}

floor vs round double dbl = Double.parseDouble(result);

Peace, have a good day.