格式化双到小数点后两位

编程入门 行业动态 更新时间:2024-10-24 03:26:16
本文介绍了格式化双到小数点后两位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

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

双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));

更多推荐

格式化双到小数点后两位

本文发布于:2023-11-10 06:17:32,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1574583.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:小数点   两位

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!