格式化双到小数点后两位 [英] Formatting a double to two decimal places

查看:94
本文介绍了格式化双到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直试图让这个打印出来是到小数点后两位的回答。所涉及的所有数学有留在两位小数的格式。我已经尝试了一些事情,我不知道该怎么改,使这项工作。

 双pdt1 = 239.99;
双pdt1Total;
双pdt2 = 129.75;
双pdt2Total;
双pdt3 = 99.95;
双pdt3Total;
双pdt4 = 350.89;
双pdt4Total;
双工资= 200;
双百分比= 9;
双总;
双答复;
双I = 100;
双A;
双B:
双C;
双D;
Console.Write(输入产品#1号售出:);
一个= Convert.ToInt32(到Console.ReadLine());Console.Write(输入产品#2号出售);
B = Convert.ToInt32(到Console.ReadLine());Console.Write(输入产品#3号出售:);
C = Convert.ToInt32(到Console.ReadLine());Console.Write(输入产品#4号卖:);
D = Convert.ToInt32(到Console.ReadLine());pdt1Total = A * pdt1;
pdt2Total = B * pdt2;
pdt3Total = C * pdt3;
pdt4Total = D * pdt4;总=(pdt1Total + pdt2Total + pdt3Total + pdt4Total);的String.Format({0:0.00},总数);
的String.Format({0:0.00},回答=(总*百分比/ I)+工资);
Console.WriteLine(本周收益:+答案+);


解决方案

即使你叫的String.Format 也不会改变原有的价值。但它会返回格式化字符串。所以请尝试以下

  Console.WriteLine(本周收益:+的String.Format({0:0.00},答案));

I have been trying to make the answer this prints out to be to two decimal places. All the math involved has to stay at that format of two decimal places. I have tried a few things and I am not sure what to change to make this work.

double pdt1 = 239.99;
double pdt1Total;
double pdt2 = 129.75;
double pdt2Total;
double pdt3 = 99.95;
double pdt3Total;
double pdt4 = 350.89;
double pdt4Total;
double wage = 200;
double percentage = 9;
double total;
double answer;
double i = 100;
double a;
double b;
double c;
double d;


Console.Write("Enter number sold of product #1: ");
a = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number sold of product #2: ");
b = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number sold of product #3: ");
c = Convert.ToInt32(Console.ReadLine());

Console.Write("Enter number sold of product #4: ");
d = Convert.ToInt32(Console.ReadLine());



pdt1Total = a * pdt1;
pdt2Total = b * pdt2;
pdt3Total = c * pdt3;
pdt4Total = d * pdt4;

total = (pdt1Total + pdt2Total + pdt3Total + pdt4Total);



string.Format("{0:0.00}", total);
string.Format("{0:0.00}", answer = (total * percentage / i) + wage);


Console.WriteLine("Earnings this week: "+answer+"");

解决方案

even you call string.Format it will not change original value . but it will return formatted string. so try below

 Console.WriteLine("Earnings this week: " +string.Format("{0:0.00}", answer));

这篇关于格式化双到小数点后两位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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