Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

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?

Please help me .

Thank You .
 
Reply With Quote
 
 
 
 
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


 
Reply With Quote
 
 
 
 
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.

See also http://mindprod.com/jgloss/floatingpoint.html

--
Canadian Mind Products, Roedy Green.
Coaching, problem solving, economical contract programming.
See http://mindprod.com/jgloss/jgloss.html for The Java Glossary.
 
Reply With Quote
 
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
 
Reply With Quote
 
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
 
Reply With Quote
 
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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
float to string to float, with first float == second float Carsten Fuchs C++ 45 10-08-2009 09:47 AM
Error: Cannot convert Decimal("0.0000") to Decimal Vitaliy Python 1 05-29-2008 10:36 AM
TypeError: unsupported operand type(s) for -: 'Decimal' and 'Decimal'. Why? Gilbert Fine Python 8 08-01-2007 01:58 AM
Decimal to Packed Decimal Conversion in C++ Ven C++ 3 08-01-2006 03:56 PM
Re: float->byte->float is same with original float image. why float->ubyte->float is different??? bd C Programming 0 07-07-2003 12:09 AM



Advertisments