如何将java中的信息表格式化为百分之一? |表格格式java。 [英] How do I format a table of information in java only to the hundredth? | Tabular format java.

查看:64
本文介绍了如何将java中的信息表格式化为百分之一? |表格格式java。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写一个程序,从文件中读取库存项目并添加标记费用。我假设每一行都是正确的,不使用任何验证。但是,我正在以每条线的格式构建程序HOPEFULLY:名称数量成本标记。

成本是公司为项目支付的批发成本(每个)。标记是介于1和100之间的数字,表示用于确定零售成本的标记百分比。我正在尝试将所有输入数据输出到文件,以及价格,批发商品的价值(数量*成本),零售商品的价值(数量*价格)。所有货币应在输出(printf)上四舍五入为两位小数。该文件应使用printf语句以表格形式很好地格式化输出,并包含标题行。

我正在努力弄清楚如何使输出文件显示所有输入的百分之一,排除数量行。其次,我不能真正围绕'价格(R)`它抛出'Value(R)`并且现在根据windows计算器&我的数学朋友看了但却不知道代码。

{我的代码}

------------

I am working on writing a program that reads inventory items from a file and adds a markup charge. I am assuming each line will be correct and not using any validation. However, I am building the program on the format of each line HOPEFULLY being: name quantity cost markup.
The cost is the wholesale cost the company paid for the item(per each). The markup is a number between 1 and 100 which indicates the percentage of the markup to determine the retail cost. I'm trying to Output all input data to a file, along with the price, the value of the items wholesale (quantity * cost), the value of the items retail (quantity * price). All currencies should be rounded to two decimals on output (printf). The file should be nicely formatted in tabular form using printf statements for output, and include a title row.
I am struggling to figure out how I can make the output file display hundredths for all of the inputs, excluding the Quantity row. Secondly, I can't really round the 'price (R)` being that it throws off the `Value (R)` and right now it is correct according to windows calculator & my math friend who looked at it but doesn't know the code.
{MY CODE}
------------

package realplayground;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.util.Scanner;
public class Pleasegod {
public static void main(String[] args) throws FileNotFoundException {
int Quantity=0, countofQ=0, countofLines=0, count=0;
double Cost=0, Markup, FinCost=0, TotalWValue=0, TotalRValue=0, MarkPerct=0, MarkAdd=0,Value=0;
String Line, item1, item2, item3, item4;
String[] Sect;
Scanner inFile;
PrintWriter outFile;
System.out.println("(Basic) Inventory Program Starting. Opening file...");
System.out.println("Please ensure data is entered per line in the format of: 'Name Quantity Cost Markup' ");
inFile = new Scanner(new File("input.txt"));
outFile = new PrintWriter(new File("output.txt"));
outFile.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%n",
"Item", "Quantity", "Cost", "Markup %", "Price(R)", "Value (W)", "Value (R)");
outFile.println
("---------------------------------------------------------------------------------------------------------------");
while(inFile.hasNext()){
Line = inFile.nextLine();
Sect = Line.split(" ");
count++;
if(Sect.length == 4) {
item1 = Sect[0];
item2 = Sect[1];
item3 = Sect[2];
item4 = Sect[3];
Quantity = Integer.parseInt(item2);
Cost = Double.parseDouble(item3);
Markup = Double.parseDouble(item4);
Value = Quantity * Cost;
MarkPerct = Markup/100;
FinCost = CalculateRetail(Cost, Markup, MarkPerct, MarkAdd, FinCost);
double ValueMarkup = Quantity * FinCost;
ValueMarkup = Math.round(ValueMarkup * 100.0) / 100.0;
countofQ += Quantity;
countofLines = count;
TotalWValue += Value;
TotalRValue += ValueMarkup;
outFile.printf("%-10s%-10s%-10s%-10s%-10s%-10s%-10s%n", Sect[0], Sect[1], Cost, Markup, FinCost, Value, ValueMarkup);
}
else {
outFile.println("Line did not have 4 item(s)");
}
}
outFile.close();
inFile.close();
System.out.println("");
System.out.printf("Total Wholesale-Cost of all items: %1.2f", TotalWValue);
System.out.println("");
System.out.printf("Total Retail-Cost of all items: %1.2f", TotalRValue);
System.out.println("");
System.out.println("Total # of data lines: " + countofLines);
System.out.println("Total # of items: " + countofQ);
}
public static double CalculateRetail(double Cost, double Markup, double MarkPerct, double MarkAdd, double FinCost){
MarkAdd = MarkPerct * Cost;
return FinCost = MarkAdd + Cost;
}
}





{INPUT FILE}

----- -------

thingy 22 15.65 45.00

Whatcha 59 67.99 25.5

gizmo 10 100.00 40.00



错误

trythis 10 92.9999999999999999999999999999999999 10



{OUTPUT FILE}

---------------------

商品数量成本加价%价格(R)价值(W)价值(R)

---------------------------------------------- -------------------------------------------------- ---------------

thingy 22 15.65 45.0 22.692500000000003344.3 499.24

Whatcha 59 67.99 25.5 85.32745 4011.41 5034.32

gizmo 10 100.0 40.0 140.0 1000.0 1400.0

该行没有4项

trythis 10 93.0 10.0 102.3 930.0 1023.0



我尝试过的事情:



我试过四舍五入`FinCost`但这会抛弃我的已发布的{OUTPUT FILE}上的值(R)当前是正确的。添加小数格式但我遇到了与Value(R)相同的问题。我在某种意义上只是试图掩盖数字22.692500000000003使它22.69但仍然在我的计算中使用整数?此外,我有任何老教师看这个,他告诉我,我的价值(R)是正确的,但没有时间来帮我调试。谢谢。



{INPUT FILE}
------------
thingy 22 15.65 45.00
Whatcha 59 67.99 25.5
gizmo 10 100.00 40.00

error
trythis 10 92.9999999999999999999999999999999999 10

{OUTPUT FILE}
---------------------
Item Quantity Cost Markup % Price(R) Value (W) Value (R)
---------------------------------------------------------------------------------------------------------------
thingy 22 15.65 45.0 22.692500000000003344.3 499.24
Whatcha 59 67.99 25.5 85.32745 4011.41 5034.32
gizmo 10 100.0 40.0 140.0 1000.0 1400.0
The line did not have 4 item(s)
trythis 10 93.0 10.0 102.3 930.0 1023.0

What I have tried:

I have tried rounding `FinCost` but this throws off my Value (R) which is currently correct on the {OUTPUT FILE} posted. Adding decimal format but I hit the same issue with Value (R). I in a sense am just trying to mask the number 22.692500000000003 making it 22.69 but still use the whole number in my calculations? Also, I had any old teacher look at this and he told me that my Value (R) was correct but didn't really have time to help me debug. Thank you.

推荐答案

尝试使用% - 10.2f 而不是% - 10s 表示 outFile.printf()中的相关数字值。
Try using %-10.2f instead of %-10s for relevant number values in outFile.printf().


这篇关于如何将java中的信息表格式化为百分之一? |表格格式java。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆