在除法时以十进制显示答案

在除法时以十进制显示答案

本文介绍了在除法时以十进制显示答案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将a/b除以十进制显示结果
例如:我将10/3除以,结果应为3.3333,但我的项目仅显示3我可以做什么.
我正在将我的代码编写为

I am trying to divide a/b and show the result in decimal
for eg: i am dividing 10/3 and the result should come 3.3333 but mine project shows 3 only what can i do.
i am writing mine code as

outputtextblock1.Text = Convert.ToString(Convert.ToInt32(Decimal.Parse(cf1.Text))/Convert.ToInt32(Decimal.Parse(cf2.Text)));

推荐答案

decimal a = 10 / (decimal)3;


您也可以在数字后面加上一个"m",以表示它是十进制类型.如果要将其转换为 float double ,请改用"f".
示例:


You can also put an "m" after a number to indicate it''s a decimal type. If you wanted to convert it to float or double, you use "f" instead.
Example:

decimal a = 10 / 3m;


您还可以调用Convert函数:


You can also call Convert functions:

decimal a = 10 / Convert.ToDecimal(3);


在这些情况下,只需要强制转换您的一个值,因为两个变量都是数字.
另一方面,在您的代码中,您将必须同时强制转换两者,因为您无法分割字符串.

将其应用于您的代码,可能是这样的:


In these cases it''s only needed to cast only one of your values, as both variables are numeric.
In your code, on the other hand, you''ll need to cast both as you can''t divide a string.

Applying that to your code, it could be something like this:

outputtextblock1.Text = (Convert.ToDecimal(cf1.Text) / Convert.ToDecimal(cf2.Text)).ToString()


或者像这样:


Or maybe like this:

outputtextblock1.Text = (Decimal.Parse(cf1.Text) / Decimal.Parse(cf2.Text)).ToString();



我希望这有帮助. :)



I hope this helps. :)



string a = "10";
string b = "3";

double result = double.Parse(a) / double.Parse(b);

outputtextblock1.Text = result.ToString();




瓦莱里.




Valery.


这篇关于在除法时以十进制显示答案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-24 10:29