在除法时以十进制显示答案 [英] To show the answer in decimal while dividing

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

问题描述

我正在尝试将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)));

推荐答案

除以两个整数时,只会得到该除法的整数部分.
更改它的最简单方法是:
When you divide two integers, you''ll get only the integer part of that division.
The easiest way to change it is:
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. :)


您正在将除数和除数都转换为Int32,然后然后执行除法,因此整数除法是已执行.
例如,您可以删除Convert.ToInt32调用.
You are converting both the dividend and divisor to Int32 and then performing the division, hence a integer division is performed.
You may, for instance, remove the Convert.ToInt32 calls.


您好,

您可以这样做:

Hello,

You could do it like this:

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

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

outputtextblock1.Text = result.ToString();




瓦莱里.




Valery.


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

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