简体   繁体   中英

How to this percentage to a whole number with decimal places

Hi all I have script code which works perfectly and display a pie chart percentages. The percentage displays 15 decimal places and I want this to make it a whole number. Here's my code. Thanks for the help.

<script type="text/javascript">
    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Total Number of Patients Per Month'
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                }
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ this.percentage +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        }

        $.getJSON("data.php", function(json) {
            options.series[0].data = json;
            chart = new Highcharts.Chart(options);
        });



    });   
    </script>

The chart displays with 15 decimal places, like 15.123456789123456%.

Math.round(15.123456789123456)
15

Math.floor(15.123456789123456)
15

Math.ceil(15.123456789123456)
16

replace 15.123456789123456 with this.percentage

Use

Math.floor() 

javascript function. You should update your code when the percentage text is added, so:

<script type="text/javascript">
    $(document).ready(function() {
        var options = {
            chart: {
                renderTo: 'container',
                plotBackgroundColor: null,
                plotBorderWidth: null,
                plotShadow: false
            },
            title: {
                text: 'Total Number of Patients Per Month'
            },
            tooltip: {
                formatter: function() {
                    return '<b>'+ this.point.name +'</b>: '+ Math.floor(this.percentage) +' %';
                }
            },
            plotOptions: {
                pie: {
                    allowPointSelect: true,
                    cursor: 'pointer',
                    dataLabels: {
                        enabled: true,
                        color: '#000000',
                        connectorColor: '#000000',
                        formatter: function() {
                            return '<b>'+ this.point.name +'</b>: '+ Math.floor(this.percentage) +' %';
                        }
                    }
                }
            },
            series: [{
                type: 'pie',
                name: 'Browser share',
                data: []
            }]
        }

        $.getJSON("data.php", function(json) {
            options.series[0].data = json;
            chart = new Highcharts.Chart(options);
        });



    });   
    </script>
parseInt( this.percentage )

这将仅提供整数部分,尽管与您的需求无关,但也适用于负数。

As we're inside a JavaScript function, how about Number.prototype.toFixed()

return '<b>'+ this.point.name +'</b>: '+ this.percentage.toFixed(0) +' %';

Read on here: MDN Link

See example here: Codepen

How about using Math.floor(value)

the Html

 <p>
    <input type="text" id="source" value="15.123456789123456789">
    <input type="text" id="target" value="">
</p>
<input type="submit" id="floorIt" value="To Integer">

the JS

$(document).ready(function () {
    $("#floorIt").click(function (e) {
        var sourceValue = $("#source").val();
        var targetValue = Math.floor(sourceValue);

        $("#target").val(targetValue);
    });

});

take a look at this Fiddle

要将数字四舍五入到两位小数,可以使用以下命令:

this.percentage = Math.round(this.percentage * 100) / 100;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM