Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,144 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,075 people online right now. Registration is fast and FREE... Join Now!




Removing the Zero After the Decimal Point

 
Reply to this topicStart new topic

Removing the Zero After the Decimal Point

Amy.A
post 11 Oct, 2008 - 01:48 AM
Post #1


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 5

Hi,

How can I remove the zero that appears after the decimal point.
For example, I have 7.0 and I need the 7 only....

I used the DecimalFormat, but couldn't remove the zeros.

Any hint plz?

Thnx in advance....
User is offlineProfile CardPM

Go to the top of the page

David W
post 11 Oct, 2008 - 03:37 AM
Post #2


D.I.C Regular

Group Icon
Joined: 20 Sep, 2008
Posts: 306



Thanked 15 times

Dream Kudos: 250
My Contributions


QUOTE(Amy.A @ 11 Oct, 2008 - 02:48 AM) *

Hi,

How can I remove the zero that appears after the decimal point.
For example, I have 7.0 and I need the 7 only....

I used the DecimalFormat, but couldn't remove the zeros.

Any hint plz?

Thnx in advance....


You could convert to an integer like this for C++

int i = int( 7.0 + 0.5 ) // to handle numbers that are really stored in memory as 6.9999999 but print out as 7.0
User is offlineProfile CardPM

Go to the top of the page

Amy.A
post 11 Oct, 2008 - 05:30 AM
Post #3


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 5

It is not a matter of memory... It is a matter of output...
All in all... Thnx David W
User is offlineProfile CardPM

Go to the top of the page

MrMoke
post 11 Oct, 2008 - 06:25 AM
Post #4


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 2


My Contributions


QUOTE(Amy.A @ 11 Oct, 2008 - 02:48 AM) *

Hi,

How can I remove the zero that appears after the decimal point.
For example, I have 7.0 and I need the 7 only....

I used the DecimalFormat, but couldn't remove the zeros.

Any hint plz?

Thnx in advance....


The easiest way in Java without changing the variable would be things like this.

double x = 45.65;
System.out.println(Math.round(x)); // Would round up to 46 for print only

system.out.println((int)x); // Would print 45

x = 45.45;
System.out.println(Math.round(x)); // Would not round up
User is offlineProfile CardPM

Go to the top of the page

Kingbradley6
post 11 Oct, 2008 - 06:29 AM
Post #5


D.I.C Head

Group Icon
Joined: 10 Jul, 2008
Posts: 116



Thanked 1 times

Dream Kudos: 100
My Contributions


Did you mean this...

CODE

double yourVariable; //initialise your variable
yourVariable = 7.0; //give it a real value of 7.0
System.out.println((int)yourVariable); //outputs 7


If this is what you mean then you need to use the below as this basically just drops anything after the decimal point.

CODE

(int)double_name


This post has been edited by Kingbradley6: 11 Oct, 2008 - 06:38 AM
User is offlineProfile CardPM

Go to the top of the page

Amy.A
post 11 Oct, 2008 - 07:27 AM
Post #6


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 5

Thnx both... but I am looking for something that can recognize the zero after the decimal point, then remove it...

I have declared two float variables for addition purpose...

So, the user can input 2.2+2.2 and get 4.4
And sometimes the user will enter 2+2 and will get 4.0>> this zero needs to be deleted...

That's why I can't use the round method nor converting the whole result into an integer...

Any hint....
sad.gif
User is offlineProfile CardPM

Go to the top of the page

Gloin
post 11 Oct, 2008 - 07:38 AM
Post #7


On MeD.i.Cation

Group Icon
Joined: 4 Aug, 2008
Posts: 717



Thanked 46 times
My Contributions


http://java.sun.com/j2se/1.4.2/docs/api/ja...imalFormat.html

Possibly you'll find the answer in the API above
User is offlineProfile CardPM

Go to the top of the page

pbl
post 11 Oct, 2008 - 07:39 AM
Post #8


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,946



Thanked 186 times

Dream Kudos: 75
My Contributions


QUOTE(Amy.A @ 11 Oct, 2008 - 08:27 AM) *

Thnx both... but I am looking for something that can recognize the zero after the decimal point, then remove it...

I have declared two float variables for addition purpose...

So, the user can input 2.2+2.2 and get 4.4
And sometimes the user will enter 2+2 and will get 4.0>> this zero needs to be deleted...

That's why I can't use the round method nor converting the whole result into an integer...

Any hint....
sad.gif


So multiply by 10
Get the modulo and print it if it is not 0

CODE


float x = 2.2;
float y = x * 10.0;
int i = (int) y;
int mod = i % 10;

if(mod == 0)
   System.out.println((i/10) + ".");
else
   System.out.println((i/10) + "." + mod);

User is offlineProfile CardPM

Go to the top of the page

Amy.A
post 11 Oct, 2008 - 08:41 AM
Post #9


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 5

Thnx...

I am very grateful...


This post has been edited by Amy.A: 11 Oct, 2008 - 08:42 AM
User is offlineProfile CardPM

Go to the top of the page

MrMoke
post 12 Oct, 2008 - 04:38 PM
Post #10


New D.I.C Head

*
Joined: 11 Oct, 2008
Posts: 2


My Contributions


Remembering that ints are 32-bit, and doubles are 64-bit, maybe try these.

double y=1212345.0;
if(y == (long)y)
System.out.println((long)y + ".");
else
System.out.println (y);
User is offlineProfile CardPM

Go to the top of the page

bbq
post 13 Oct, 2008 - 01:42 AM
Post #11


D.I.C Head

Group Icon
Joined: 15 May, 2008
Posts: 183



Thanked 13 times

Dream Kudos: 50
My Contributions


Another option so that you are not casting to int is the following
java

double weight = 70;

System.out.println("Weight : " + weight);
System.out.println("Weight formatted : " + String.format("%.0f", weight) );


Output
QUOTE
Weight : 70.0
Weight formatted : 70


the String.format("%.0f", weight) method is useful for this sort of thing... the "%.0f" indicates 0 decimal places if it were to be "%.1f" it would print the same as the statement above

wink2.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/21/08 12:45PM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month