将int转换为float

时间:2012-11-23 13:34:44

标签: c int

int total=0, number=0;
float percentage=0.0;

percentage=(number/total)*100;
printf("%.2f",percentage);

如果数字的值是50且总数是100,那么我应该得到50.00的百分比,至少这是我想要的。但我一直得到0.00作为答案,并尝试了很多类型的改变,但没有用。

9 个答案:

答案 0 :(得分:25)

整数除法截断,因此(50/100)会导致0.您可以转换为float(更好double)或乘以100.0double精度,100.0f精度为float,首先,

double percentage;
// ...
percentage = 100.0*number/total;
// percentage = (double)number/total * 100;

float percentage;
// ...
percentage = (float)number/total * 100;
// percentage = 100.0f*number/total;

由于浮点运算不是关联的,100.0*number/total(double)number/total * 100的结果可能略有不同(同样适用于float),但它极不可能影响前两个小数点后面的位置,所以你选择哪种方式可能无关紧要。

答案 1 :(得分:6)

C中的整数除法会截断结果,因此50/100将为您提供0

如果您想获得所需的结果,请尝试以下方法:

((float)number/total)*100

50.0/100

答案 2 :(得分:2)

不,因为你使用整数表达式,所以你将整数 50除以整数 100,这会产生整数 0.键入其中一个到float,它应该可以工作。

答案 3 :(得分:1)

您正在进行整数运算,因此结果是正确的。尝试

percentage=((double)number/total)*100;

BTW %f期望double不是float。通过在这里转换的纯粹运气,它运作良好。但是现在你通常在C中主要使用double作为浮点类型。

答案 4 :(得分:1)

如果我想要浮点数,我会经常乘以1.0,这比记住规则更容易。

答案 5 :(得分:0)

这可以为您提供所需的结果。

double total = 0;
int number = 0;
float percentage = number / total * 100
printf("%.2f",percentage);

请注意,第一个操作数是双

答案 6 :(得分:0)

将您的代码更改为:

int total=0, number=0;
float percentage=0.0f;

percentage=((float)number/total)*100f;
printf("%.2f", (double)percentage);

答案 7 :(得分:0)

这可以为您提供正确的答案

#include <stdio.h>
int main()
{
    float total=100, number=50;
    float percentage;
    percentage=(number/total)*100;
    printf("%0.2f",percentage);
    return 0;
}

答案 8 :(得分:0)

这应该可以使其圆整到2点

if ($query->rowCount() > 0) {
        foreach ($results as $result) {

            ?>
                                <tr>

                                    <td width="10"><?php echo htmlentities($result->sr_no);?></td>
                                    <td width="10"><?php echo htmlentities($result->code);?></td>
                                    <td width="10"><?php echo htmlentities($result->unit_name);?></td>
                                    <?php
            $status = $result->status;
            if ($status == 'TRUE') {
                ?>
                                    <td class="v-align-middle">
                                        <div class="checkbox check-primary">
                                            <input type="checkbox" value="" id="status_true" checked> <label
                                                for="status_true"></label>
                                        </div>
                                    </td>
                                    <?php
            } else {

                ?>
                                    <td class="v-align-middle">
                                        <div class="checkbox check-primary">
                                            <input type="checkbox" value="" id="status_false"> <label
                                                for="status_false"></label>
                                        </div>
                                    </td>
                                    <?php
            }
            ?>
        <td>

        <?php  $result->code;?>
                             <button type="button" name="modify"
                                            class="btn btn-primary btn-sm btn-small" data-toggle="modal"
                                            data-target="#myModal"
                                            data-id="<?php $code_send=$result->code;?>">Modify</button> 

                <?php

            $sql = "SELECT * FROM `unit_master` where code='$code_send'";
            $query = $dbh->prepare($sql);
            $query->execute();
            $results = $query->fetchAll(PDO::FETCH_OBJ);
            $cnt = 1;
            if ($query->rowCount() > 0) {
                foreach ($results as $result) {
                    $code = $result->code;
                    $unit_name = $result->unit_name;
                    $abbbr = $result->abbreviation;
                    $app_from = $result->applicable_from;
                    $opd_no = $result->OPD_No;
                    $con_no = $result->concession_no;
                    $free_no = $result->freeship_no;
                    $credt_note_no = $result->credit_note_no;
                    $debit_note_no = $result->debit_note_no;
                    $ipd_no = $result->IPD_no;
                    $ipd_no_prefix = $result->IPD_no_prefix;
                    $receipt_no = $result->receipt_no;
                    $corp_receipt_no = $result->corp_receipt_no;
                    $adv_receipt_no = $result->adv_receipt_no;
                    $refund_receipt_no = $result->refund_receipt_no;
                }
            }
            ?>  

                                            <!-- Modal -->
                                        <form name="update" id="update" method="post"
                                            action="userindex.php" enctype="multipart/form-data">
                                            <input type="hidden" name="q" value="unit_master" /> <input
                                                type="hidden" name="flg" value="2">

                                            <div class="modal fade" id="myModal" tabindex="-1"
                                                role="dialog" aria-labelledby="myModalLabel"
                                                aria-hidden="true">
                                                <div class="modal-dialog">
                                                    <div class="modal-content">
                                                        <div class="modal-header">
                                                            <button type="button" class="close" data-dismiss="modal"
                                                                aria-hidden="true">X</button>

                                                            <h4 id="myModalLabel" class="semi-bold">Modify Unit</h4>

                                                            <br>
                                                        </div>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $code?>" placeholder="Code"
                                                                        name="code" id="code">
                                                                </div>
                                                                <div class="col-md-8">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $unit_name?>" placeholder="Name"
                                                                        name="unit_name" id="unit_name">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $abbbr?>"
                                                                        placeholder="Abbriviation" name="Abbriviation"
                                                                        id="Abbriviation">
                                                                </div>

                                                                <div class="col-md-3">
                                                                    <label> Aplicable From</label>

                                                                </div>
                                                                <div class="col-md-5">
                                                                    <div
                                                                        class="input-append success date col-md-10 col-lg-6 no-padding">
                                                                        <input type="text" class="form-control"
                                                                            value="<?php echo $app_from?>"> <span class="add-on"><span
                                                                            class="arrow" name="applicable_from"
                                                                            id="applicable_from"></span><i class="fa fa-th"></i></span>
                                                                    </div>
                                                                </div>
                                                            </div>

                                                        </div>

                                                        <center>
                                                            <h4 id="myModalLabel" class="semi-bold">OPD</h4>
                                                        </center>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $opd_no?>" placeholder="OPD No"
                                                                        name="opd_no" id="opd_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $con_no?>"
                                                                        placeholder="Concession No" name="concession_no"
                                                                        id="concession_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $free_no?>"
                                                                        placeholder="Freeship No" name="freeship_no"
                                                                        id="freeship_no">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $credt_note_no?>"
                                                                        placeholder="Credit Note No" name="credit_note_no"
                                                                        id="credit_note_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $debit_note_no?>"
                                                                        placeholder="Debit Note No" name="debit_note_no"
                                                                        id="debit_note_no">
                                                                </div>

                                                                <div class="col-md-2">
                                                                    <div class="checkbox check-primary">
                                                                        <input type="checkbox" value="1"
                                                                            id="checkboxIsDialysis"> <label
                                                                            for="checkboxIsDialysis">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;IsDialysis</label>

                                                                    </div>
                                                                </div>



                                                            </div>


                                                        </div>


                                                        <center>
                                                            <h4 id="myModalLabel" class="semi-bold">IPD</h4>
                                                        </center>
                                                        <div class="modal-body">
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $ipd_no?>" placeholder="IPD No"
                                                                        name="ipd_no" id="ipd_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $ipd_no_prefix?>"
                                                                        placeholder="IPD No Prefix" name="ipd_no_prefix"
                                                                        id="ipd_no_prefix">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $receipt_no?>"
                                                                        placeholder="Receipt No" name="receipt_no"
                                                                        id="receipt_no">
                                                                </div>

                                                            </div>
                                                            <div class="row form-row">
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $corp_receipt_no?>"
                                                                        placeholder="Corp Receipt No" name="corp_receipt_no"
                                                                        id="corp_receipt_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $adv_receipt_no?>"
                                                                        placeholder="Adv Receipt No" name="adv_receipt_no"
                                                                        id="adv_receipt_no">
                                                                </div>
                                                                <div class="col-md-4">
                                                                    <input type="text" class="form-control"
                                                                        value="<?php echo $refund_receipt_no?>"
                                                                        placeholder="Refund Receipt No"
                                                                        name="refund_receipt_no" id="refund_receipt_no">
                                                                </div>




                                                            </div>


                                                        </div>

                                                        <div class="modal-footer">
                                                            <button type="button" class="btn btn-default"
                                                                data-dismiss="modal">Close</button>


                                                            <button type="submit" class="btn btn-primary">Save
                                                                changes</button>



                                                        </div>
                                                    </div>
                                                    <!-- /.modal-content -->
                                                </div>
                                                <!-- /.modal-dialog -->
                                            </div>
                                            <!-- /.modal -->



                                            <!-- <button type="button" class="btn btn-danger btn-sm btn-small">Delete</button> -->

                                    </td>

                                    <!--    <td width="70"><a href="functions_upload.php?eid=val=5">Delete</a></td>-->

                                </tr>
                                </form>