Conversions

• 05-25-2012, 05:15 AM
shahin912
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?
• 05-25-2012, 05:45 AM
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.

Code:

`System.out.printf("%, d", new BigInteger("640237370528000"));`
• 05-25-2012, 05:56 PM
shahin912
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
• 05-25-2012, 06:00 PM
JosAH
Re: Conversions

kind regards,

Jos
• 05-25-2012, 06:11 PM
shahin912
Re: Conversions
yes i did, i used that. i got java.util.illegalformatconversionexception
• 05-25-2012, 06:21 PM
JosAH
Re: Conversions
Quote:

Originally Posted by shahin912
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
• 05-25-2012, 06:36 PM
shahin912
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
• 05-25-2012, 06:40 PM
JosAH
Re: Conversions
That format String is a mess; copy the format String given to you verbatim and see the result.

kind regards,

Jos