6 Replies - 710 Views - Last Post: 30 October 2008 - 07:58 AM Rate Topic: -----

#1 side  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 08

DecimalFormat

Post icon  Posted 29 October 2008 - 08:35 AM

i wanna change integer to binary number

can i use DecimalFormat class??

i look up for the API, but im not completely understand....

here are my program after i tried a bit time...

[code]
import java.text.DecimalFormat;

public class NumberFormat
{
public static void main(String args[])
{
NumberFormat f = NumberFormat.getInstance(z);
if (f instanceof DecimalFormat)
{
((DecimalFormat) f).setDecimalSeparatorAlwaysShown(true);
}
}
}


hope someone can help me!!

Is This A Good Question/Topic? 0
  • +

Replies To: DecimalFormat

#2 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: DecimalFormat

Posted 29 October 2008 - 08:39 AM

You need to do

Integer.toString(number, 2);



or

Integer.toBinaryString(number);



as DecimalFormat is for floating point formatting

This post has been edited by g00se: 29 October 2008 - 08:44 AM

Was This Post Helpful? 0
  • +
  • -

#3 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: DecimalFormat

Posted 29 October 2008 - 04:41 PM

View Postg00se, on 29 Oct, 2008 - 08:39 AM, said:

as DecimalFormat is for floating point formatting

and for integer format and for locale format
Was This Post Helpful? 0
  • +
  • -

#4 g00se  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2689
  • View blog
  • Posts: 11,351
  • Joined: 20-September 08

Re: DecimalFormat

Posted 29 October 2008 - 05:00 PM

Quote

and for integer format and for locale format



Quite right. In fact I should probably have said 'formatting', period, since binary output is in a way not really formatting: it doesn't even figure in printf, for instance
Was This Post Helpful? 0
  • +
  • -

#5 side  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 08

Re: DecimalFormat

Posted 29 October 2008 - 06:35 PM

View Postg00se, on 29 Oct, 2008 - 05:00 PM, said:

Quote

and for integer format and for locale format



Quite right. In fact I should probably have said 'formatting', period, since binary output is in a way not really formatting: it doesn't even figure in printf, for instance


thx for ur help

but, i still not really understand.......

could you explain clearly? pls
Was This Post Helpful? 0
  • +
  • -

#6 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8329
  • View blog
  • Posts: 31,857
  • Joined: 06-March 08

Re: DecimalFormat

Posted 29 October 2008 - 07:40 PM

for(int i = 0; i <= 7; i++)
	System.out.println("Dec: " + i + " = " + Integer.toString(i, 2));


would print
Dec 0 = 0
Dec 1 = 1
Dec 2 = 10
Dec 3 = 11
Dec 4 = 100
Dec 5 = 101
Dec 6 = 110
Dec 7 = 111

This post has been edited by pbl: 29 October 2008 - 07:42 PM

Was This Post Helpful? 1
  • +
  • -

#7 side  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 28-October 08

Re: DecimalFormat

Posted 30 October 2008 - 07:58 AM

View Postpbl, on 29 Oct, 2008 - 07:40 PM, said:

for(int i = 0; i <= 7; i++)
	System.out.println("Dec: " + i + " = " + Integer.toString(i, 2));


would print
Dec 0 = 0
Dec 1 = 1
Dec 2 = 10
Dec 3 = 11
Dec 4 = 100
Dec 5 = 101
Dec 6 = 110
Dec 7 = 111


thank u very very much!!!

caz im not familiar with java....

so ur example is really helpful^^
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1