为什么我用这段代码得到0.0?

时间:2016-10-04 17:23:29

标签: c# math percentage

当我进入Windows计算器实用程序时," 15036/18218 * 100 ="它返回82.53375782193435

我真正想要的是17.47(100 - 82.53),但目前还没有。

使用此代码:

// Example: thisQty == 3182; totalQty == 18218
private string GetPercentage(int thisQty, int totalQty)
{
    int diff = totalQty - thisQty; // this equates to 15036
    double prcntg = (diff/totalQty)*100; // this equates to 0.0 for some reason
    return string.Format("{0}%", prcntg);
}

...我的prcntg值为0.0。为什么? ISTM,这与我在Calculator实用程序中手动完成的操作相同。为什么它不返回82.53375782193435?

3 个答案:

答案 0 :(得分:5)

即使正确的数学答案是分数,2 int s的除法也是int

为了保持小数部分,你必须除以一个包含小数部分的类型(如doubledecimal):

Console.WriteLine(GetPercentage(3182, 18218));

private string GetPercentage(int thisQty, int totalQty)
{
   int diff = totalQty - thisQty; // this equates to 15036
   double prcntg = (diff / (double)totalQty) * 100;
   return string.Format("{0}%", prcntg);
}

顺便说一句 - 如果你将double difftotalQty强加给/并不重要 - 因为它会执行double操作返回#!/usr/bin/env bash man="Usage: namecrypt [ -h ] [ -e || -d ] [ -F ] [ -D ] [DIRECTORY] [PASSPHRASE]\n -h, --help display this message -e, --encrypt encrypt the specified directory -d, --decrypt decrypt the specified directory -F, --files include files -D, --dir include directories [DIRECTORY] relative or absolute path to a directory/symbolic link [PASSPHRASE] optional - specify the user passphrase on command line"; options=(); for Arg in "$@"; do if [ "$Arg" == "-h" ] || [ "$Arg" == "--help" ]; then echo -e "$man"; exit; elif [ "$Arg" == "-e" ] || [ "$Arg" == "--encrypt" ]; then options[0]="E"; elif [ "$Arg" == "-d" ] || [ "$Arg" == "--decrypt" ]; then options[0]="D"; elif [ "$Arg" == "-F" ] || [ "$Arg" == "--files" ]; then options[1]="${options[1]}F"; elif [ "$Arg" == "-D" ] || [ "$Arg" == "--dir" ]; then options[1]="${options[1]}D"; elif [ -d "$Arg" ]; then options[2]="$(realpath "$Arg")"; else options[3]="$Arg"; fi; done; if [ "${options[0]}" == "" ]; then echo "No Mode specified!"; exit 1; fi; if [ "${options[1]}" == "" ]; then options[1]="F"; fi; if [ "${options[2]}" == "" ]; then echo "No such directory!"; exit 2; fi; if [ "${options[3]}" == "" ]; then echo "Enter a passphrase: "; read options[3]; fi; shopt -s nullglob dotglob; function hashTarget { BASE="$(basename "$1")"; DIR="$(dirname "$1")/"; if [ -a "$1" ]; then oldName="$BASE"; newName=$(echo "$oldName" | md5sum); echo "$oldName||$newName" >> "$DIR.names"; mv "$1" "$DIR$newName"; else echo "Skipping '$1' - No such file or directory!"; fi; } function dehashTarget { BASE="$(basename "$1")"; DIR="$(dirname "$1")/"; [ -f "$DIR.names.cpt" ] && ccdecrypt -K "${options[3]}" "$DIR.names.cpt"; if [ -f "$DIR.names" ]; then oldName="$BASE"; newName=$(grep "$oldName" "$DIR.names" | awk -F '|' '{print $1}'); [[ ! -z "${newName// }" ]] && mv "$1" "$DIR$newName"; else echo "Skipping '$1' - Hash table not found!"; fi; } function mapTarget { DIR="$(dirname "$1")/"; for Dir in "$1"/*/; do mapTarget "$Dir"; done; for Item in "$1"/*; do if ([ -f "$Item" ] && [[ "${options[1]}" == *"F"* ]]) || ([ -d "$Item" ] && [[ "${options[1]}" == *"D"* ]]); then if [ "${options[0]}" == "E" ]; then hashTarget "$Item"; else dehashTarget "$Item"; fi; fi; done; [ "${options[0]}" == "D" ] && [ -f "$DIR.names" ] && rm "$DIR.names"; [ "${options[0]}" == "E" ] && [ -f "$DIR.names" ] && ccencrypt -K "${options[3]}" "$DIR.names"; } mapTarget "${options[2]}"; - 这意味着保留分数部分

答案 1 :(得分:2)

您正在使用整数值(它不会存储派系部分),因此将其强制转换为double,或将参数类型用作double(我的推荐)。您的操作15036/18218解析为0.82,并存储为0的整数值...最终0 * 100将解析为0,这是您获得结果的位置。

试试这个,

private string GetPercentage(double thisQty, double totalQty)
{
    double diff = totalQty - thisQty; // this equates to 15036
    double prcntg = (diff/totalQty) * 100.0; // this equates to 0.0 for some reason
    return string.Format("{0}%", prcntg);
}

这也有小数部分,你会得到结果。

答案 2 :(得分:1)

根据Gilad Green的回答,这是我最终得到的结果,它给出了我最终想要的值,并将值舍入为整数:

private string GetPercentage(int thisQty, int totalQty)
{
    int diff = totalQty - thisQty;
    double prcntg = (diff / (double)totalQty) * 100;
    prcntg = 100 - prcntg;
    int roundedPercent = Convert.ToInt32(prcntg);
    return string.Format("{0}%", roundedPercent);
}