双重格式问题 [英] double formatting question

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

问题描述

为什么我除以0.0时会得到各种格式

Why am I getting various formats of 0.0 , when I divide

int wins = 3;
int games = 12;
double winPercentage = 0;
winPercentage = wins/games;
TextBlock.Text= winpercentage.ToString("assorted formating experiments");


我正在尝试显示球队的胜率.
我似乎只能得到0;


I am trying to display team win percentages.
All I seem to be able to get is 0;

推荐答案

这与双格式无关.您的问题是整数除法. "3/12 = 0.25",即整数"0".
如果您尝试使用double wins = 3;double games = 12;(double)wins/gameswins/(double)games,则将其获取为"0.25".
如果您至少要提供两个操作数,则除法运算符会将这两个操作数都视为浮点数.但是由于两个操作数都是整数,所以结果不包含浮点数.
This has nothing to do with double formatting. Your problem is Integer division. "3/12 = 0.25" which is "0" as integer.
If you tried double wins = 3; or double games = 12; or (double)wins/games or wins/(double)games you''d get it as "0.25".
The division operator would treat both operands as floating point number if you''d supply at least 1 of them as such. But since both operands are integer result does not hold floating point.


执行以下操作:

Do it like this:

winPercentage = (double)wins/games;


您需要将其转换为两倍.


You need to cast it to double.


这篇关于双重格式问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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