java - 小数点如何转换?

标签 java decimal

我正在尝试制作股票计算器。老实说,我不知道如何使用

DecimalFormat df2 = new DecimalFormat(".##"); 

正确。

我正在尝试将利润值更改为小数点后两位,但我不知道需要做什么。此外,字符串 sellOut 值根本没有显示。 你能帮我解决这个问题吗?

import java.util.Scanner;
import java.io.*;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.text.DecimalFormat;

 public class Assignment
{

public static void main(String args[]) throws IOException
{

File dataFile = new File ("Feb13StockData.txt");
if(!(dataFile.exists()))
{
  System.out.println("invalid file");
  System.exit(0);
}
DecimalFormat df2 = new DecimalFormat(".##");
Scanner fileInput = new Scanner (dataFile);
String stock;
int pp, sp, ns;
double pc, sc;
String sell;
double profit;



//read first student record.
//for(int i = 1; i<= 4; i++)
System.out.println("Stock \t\t"+"PP \t\t"+"SP \t\t"+"PC \t\t"+"SC \t\t"+"NS \t\t"+"sellOut \t\t"+"profit \t\t");
while (fileInput.hasNext()){

stock = fileInput.nextLine();
pp = fileInput.nextInt();
sp = fileInput.nextInt();
pc = fileInput.nextDouble();
sc = fileInput.nextDouble();
ns = fileInput.nextInt();
sell = fileInput.nextLine();
profit = ProfitOut(pp,sp,pc,ns,sc);

printToScreen(stock,pp,sp,pc,sc,ns,sell,profit);


//System.out.println(student + "\t\t" + stYear + "\t\t" + gpa+ "\t\t" + credits);

//discard BOL CHARACTOR
fileInput.nextLine();
}

fileInput.close();
}


 public static void printToScreen(String stockOut, int ppOut, int spOut, double pcOut, double scOut, int nsOut, String sellOut, double profit){

System.out.println(stockOut+ "\t\t" + ppOut +"\t\t" + spOut+"\t\t" +pcOut+"\t\t" +scOut+"\t\t"+ nsOut+"\t\t"+sellOut+"\t\t"+profit );


}
  public static double ProfitOut (int pp, int sp, double pc, int ns, double sc)
  {
    double profit;
    return profit = ((ns * sp) - sc) - ((ns *pp) + pc);
  }


 }

字符串销售无法正常工作。 NY 根本没有显示。

这是我的文件。

AXC 25 54 9.11 6.98 20 N CLR 24 44 9.68 8.63 50 N UPQ 38 52 4.95 5.24 30 Y SLS 46 51 7.29 4.95 50 y MOP 20 32 4.95 6.58 50 N NRK 19 43 5.25 7.74 60 N COP 48 29 6.62 5.06 30 Y SRY 19 52 4.95 9.32 50 N MPL 25 36 8.3 4.95 20 y RRZ 24 51 4.95 5.46 20 N XON 14 33 4.95 7.41 40 N LSW 18 50 4.95 4.95 30 N

最佳答案

您可以使用 DecimalFormat 类格式化该值:

DecimalFormat df = new DecimalFormat("###.##");
System.out.println(df.format(PI));

使用 BigDecimal 舍入 double 要将 double 舍入到小数点后 n 位,我们可以编写一个辅助方法:

private static double round(double value, int places) {
    if (places < 0) throw new IllegalArgumentException();
    BigDecimal bd = new BigDecimal(Double.toString(value));
    bd = bd.setScale(places, RoundingMode.HALF_UP);
    return bd.doubleValue();
}

您可以使用上述方法在任意小数点处取任意值。

关于java - 小数点如何转换?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54779402/

相关文章:

java - 如何获取Azure存储中文件、表和队列的所有服务的大小?在JAVA中

java - Netty 4 - 池返回一个尚未准备好发送实际消息的 channel

javascript - 我想删除 javascript/jquery 中的小数点

php - Mysql 表更新操作无明显原因失败

Java从字节流mp3

java - 子资源在 Jersey REST API 框架中不起作用

java - 检查字段是否被覆盖

c# - NumericUpDown:接受逗号和点作为小数分隔符

php - 如何在 PHP 中过滤小数输入并对其进行清理

ruby-on-rails - rails : How to print a decimal as a percent?