Results 1 to 8 of 8

Thread: Conversions

  1. #1
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Conversions

    if i have a code like this:

    out.printf("%.0f", f);

    "f" is a big number.

    for example 640237370528000

    i want the number to be like this

    6,402,373,705,728,000 when it gets printed out.

    how do i do the conversion?

  2. #2
    wsaryada is offline Senior Member
    Join Date
    Jun 2007
    Location
    Bali, Indonesia
    Posts
    757
    Rep Power
    8

    Default Re: Conversions

    If your big number is an integer number and you are using the BigInteger class you can simply use the "%, d" as the format.

    Java Code:
    System.out.printf("%, d", new BigInteger("640237370528000"));

  3. #3
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Conversions

    the users enter a number. its not a fixed number. i want to know how to use conversions so there is a comma in every thousand separator

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conversions

    Have you actually read the previous reply?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  5. #5
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Conversions

    yes i did, i used that. i got java.util.illegalformatconversionexception

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conversions

    Quote Originally Posted by shahin912 View Post
    yes i did, i used that. i got java.util.illegalformatconversionexception
    What is the format String you used and what is your Java version?

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  7. #7
    shahin912 is offline Member
    Join Date
    May 2012
    Posts
    15
    Rep Power
    0

    Default Re: Conversions

    this is my code:

    import java.io.*;
    import java.util.Scanner;
    import java.lang.*;
    import type.lib.ToolBox;

    public class eCheck03D
    {
    public static void main(String[] args)
    {

    PrintStream out = System.out;
    Scanner in = new Scanner(System.in);

    out.print("Enter an integer ... ");
    int integer = in.nextInt();

    double f = ToolBox.factorial(integer);

    out.print(integer + "! = ");
    out.printf("%.0f%, d", f);

    double root = Math.cbrt(integer);
    out.println("");
    out.print("Cubic Root of 18 = ");
    out.printf("%.0f" , root);
    }
    }



    I have JDK 7

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,336
    Blog Entries
    7
    Rep Power
    20

    Default Re: Conversions

    That format String is a mess; copy the format String given to you verbatim and see the result.

    kind regards,

    Jos
    Last edited by JosAH; 05-25-2012 at 07:39 PM. Reason: silly typo
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. [SOLVED] Base conversions
    By rlzyoner in forum New To Java
    Replies: 3
    Last Post: 12-10-2008, 12:40 PM
  2. Conversions
    By apfroggy0408 in forum New To Java
    Replies: 3
    Last Post: 03-04-2008, 11:59 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •