使用BigIntegers

时间:2015-10-05 22:37:09

标签: java biginteger data-conversion metric

我创建了一个程序,它在公制系统中占用一个基本单位。 (比如克。)然后当你选择一个前缀时,将它改为等量。 (例如,当您选择Kilo时,1000克会将其更改为1千克。)

问题是,当我运行代码时,它总是最终为零,这让我觉得我操纵BigIntegers是错误的。 (我使用的是非常大的数字,因为有些前缀非常小或非常大,超出了通常的长数。)

以下是代码:

import java.util.Scanner;
import java.math.BigInteger;

public class BaseMetricUnits {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    System.out.println("Hello, World!");
    float meter = 1;
    float kilogram = 1;
    float second = 1;
    float ampere = 1;
    float kelvin = 1;
    float mole = 1;
    float candela = 1;
    PrefixConverter();
}
public static void PrefixConverter() 
{

    BigInteger Yocto = BigInteger.valueOf((long) 0.00000000000000000000000);
    BigInteger Zepto =  BigInteger.valueOf((long) 0.000000000000000000001);
    BigInteger Atto = BigInteger.valueOf((long) 0.000000000000000001);
    BigInteger Femto = BigInteger.valueOf((long) 0.000000000000001);
    BigInteger Pico = BigInteger.valueOf((long)0.000000000001);
    BigInteger Nano = BigInteger.valueOf((long)0.000000001);
    BigInteger Micro = BigInteger.valueOf((long)0.000001);
    BigInteger Milli = BigInteger.valueOf((long)0.001);
    BigInteger Centi = BigInteger.valueOf((long)0.01);
    BigInteger Deci = BigInteger.valueOf((long)0.1);
    BigInteger Deca = BigInteger.valueOf((long)10);
    BigInteger Hecto = BigInteger.valueOf((long)100);
    BigInteger Kilo = BigInteger.valueOf((long)1000);
    BigInteger Mega = BigInteger.valueOf((long)1000000);
    BigInteger Giga = BigInteger.valueOf((long)1000000000);
    BigInteger Tera = new BigInteger("1000000000000");
    BigInteger Peta = new BigInteger("1000000000000000");
    BigInteger Exa = new BigInteger("1000000000000000000");
    BigInteger Zetta = new BigInteger("1000000000000000000000");
    BigInteger Yotta = new BigInteger("1000000000000000000000000");

    long Amount;
    double Prefix; 
    String Units = "";
    BigInteger translatedResult;
    BigInteger Result;

    Scanner inputDevice = new Scanner(System.in);
    System.out.print("Please enter the type of unit to be used. (meters, grams, etc.) >> ");
    Units = inputDevice.next();
    System.out.print("Please enter an amount to be translated >> ");
    Amount = inputDevice.nextLong();
    System.out.print("Please choose one of the following Prefixes to translate to. ");
    System.out.print(" 1 - Yocto ");
    System.out.print(" 2 - Zepto ");
    System.out.print(" 3 - Atto ");
    System.out.print(" 4 - Femto ");
    System.out.print(" 5 - Pico ");
    System.out.print(" 6 - Nano ");
    System.out.print(" 7 - Micro ");
    System.out.print(" 8 - Milli ");
    System.out.print(" 9 - Centi ");
    System.out.print(" 10 - Deci ");
    System.out.print(" 11 - Deca ");
    System.out.print(" 12 - Hecto ");
    System.out.print(" 13 - Kilo ");
    System.out.print(" 14 - Mega ");
    System.out.print(" 15 - Giga ");
    System.out.print(" 16 - Tera ");
    System.out.print(" 17 - Peta ");
    System.out.print(" 18 - Exa ");
    System.out.print(" 19 - Zetta ");
    System.out.print(" 20 - Yotta ") ;
    Prefix = inputDevice.nextDouble();

     if(Prefix == 1)
     {
        Result = Yocto.multiply(BigInteger.valueOf(Amount));
        translatedResult = Yocto.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yocto" + Units + ".");
     }
     if(Prefix == 2)
     {
        Result = Zepto.multiply(BigInteger.valueOf(Amount));
        translatedResult = Zepto.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zepto" + Units + ".");
     }
     if(Prefix == 3)
     {
        Result = Atto.multiply(BigInteger.valueOf(Amount));
        translatedResult = Atto.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Atto" + Units + ".");
     }
     if(Prefix == 4)
     {
        Result = Femto.multiply(BigInteger.valueOf(Amount));
        translatedResult = Femto.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Femto" + Units + ".");
     }
     if(Prefix == 5)
     {
        Result = Pico.multiply(BigInteger.valueOf(Amount));
        translatedResult = Pico.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Pico" + Units + ".");
     }
     if(Prefix == 6)
     {
        Result = Nano.multiply(BigInteger.valueOf(Amount));
        translatedResult = Nano.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Nano" + Units + ".");
     }
     if(Prefix == 7)
     {
        Result = Micro.multiply(BigInteger.valueOf(Amount));
        translatedResult = Micro.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Micro" + Units + ".");
     }
     if(Prefix == 8)
     {
        Result = Milli.multiply(BigInteger.valueOf(Amount));
        translatedResult = Milli.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Milli" + Units + ".");
     }
     if(Prefix == 9)
     {
        Result = Centi.multiply(BigInteger.valueOf(Amount));
        translatedResult = Centi.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Centi" + Units + ".");
     }
     if(Prefix == 10)
     {
        Result = Deci.multiply(BigInteger.valueOf(Amount));
        translatedResult = Deci.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deci" + Units + ".");
     }
     if(Prefix == 11)
     {
        Result = Deca.multiply(BigInteger.valueOf(Amount));
        translatedResult = Deca.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Deca" + Units + ".");
     }
     if(Prefix == 12)
     {
        Result = Hecto.multiply(BigInteger.valueOf(Amount));
        translatedResult = Hecto.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Hecto" + Units + ".");
     }
     if(Prefix == 13)
     {
        Result = Kilo.multiply(BigInteger.valueOf(Amount));
        translatedResult = Kilo.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Kilo" + Units + ".");
     }
     if(Prefix == 14)
     {
        Result = Mega.multiply(BigInteger.valueOf(Amount));
        translatedResult = Mega.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Mega" + Units + ".");
     }
     if(Prefix == 15)
     {
        Result = Giga.multiply(BigInteger.valueOf(Amount));
        translatedResult = Giga.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units +  " which translates to " + translatedResult + "Giga" + Units + ".");
     }
     if(Prefix == 16)
     {
        Result = Tera.multiply(BigInteger.valueOf(Amount));
        translatedResult = Tera.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Tera" + Units + ".");
     }  
     if(Prefix == 17)
     {
        Result = Peta.multiply(BigInteger.valueOf(Amount));
        translatedResult = Peta.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Peta" + Units + ".");
     }
     if(Prefix == 18)
     {
        Result = Exa.multiply(BigInteger.valueOf(Amount));
        translatedResult = Exa.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Exa" + Units + ".");
     }
     if(Prefix == 19)
     {
        Result = Zetta.multiply(BigInteger.valueOf(Amount));
        translatedResult = Zetta.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Zetta" + Units + ".");
     }
     if(Prefix == 20)
     {
        Result = Yotta.multiply(BigInteger.valueOf(Amount));
        translatedResult = Yotta.divide(BigInteger.valueOf(Amount));
        System.out.println("You have " + Result + " " + Units + " which translates to " + translatedResult + "Yotta" + Units + ".");
     }
     else
     {
        System.out.println("Not a valid input.");
     }
    }
}

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

BigInteger只能存储整数,因此不适合此应用程序的数据类型。我强烈建议将其替换为BigDecimal

您还应该使用分数的字符串表示来初始化:

  BigDecimal Zepto =  new BigDecimal("0.000000000000000000001");
  BigDecimal Atto = new BigDecimal("0.000000000000000001");
  BigDecimal Femto = new BigDecimal("0.000000000000001");
  BigDecimal Pico = new BigDecimal("0.000000000001");
  BigDecimal Nano = new BigDecimal("0.000000001");

对此答案和问题的评论表明使用BigDecimal存储较大数字的问题。该计划说明了这不是一个问题:

import java.math.BigDecimal;

public class Test {
  public static void main(String[] args) {
    BigDecimal googol = new BigDecimal("1e100");
    System.out.println(googol);
    System.out.println(googol.add(BigDecimal.ONE));
  }
}

输出:

1E+100
10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001