EllioLintt
EllioLintt

Reputation: 97

rounding float to an integer

Hello intelligent programmers:

I have a question about rounding to the nearest 1 in python.

Below is my output:

 ============Birth Month Distribution============
            Number  Percent

 January    2       7.41
 February   2       7.41
 March      2       7.41
 April      2       7.41
 May        2       7.41
 June       2       7.41
 July       2       7.41
 August     2       7.41
 September  2       7.41
 October    2       7.41
 November   3       11.11
 December   4       14.81


==================== Histogram ====================

    |------------------------------------------------------------------------------------------------
01  |  **  **  **  **  **  **  ** 
02  |  **  **  **  **  **  **  ** 
03  |  **  **  **  **  **  **  ** 
04  |  **  **  **  **  **  **  ** 
05  |  **  **  **  **  **  **  ** 
06  |  **  **  **  **  **  **  ** 
07  |  **  **  **  **  **  **  ** 
08  |  **  **  **  **  **  **  ** 
09  |  **  **  **  **  **  **  ** 
10  |  **  **  **  **  **  **  ** 
11  |  **  **  **  **  **  **  **  **  **  **  ** 
12  |  **  **  **  **  **  **  **  **  **  **  **  **  **  ** 
    |------------------------------------------------------------------------------------------------

input:

 january=months.count(1)
 januaryPercent=float(round(((january/counter)*100),2))
 januaryNumber=int((((january/counter)*100)//1))
 januaryHistogram=januaryNumber*" ** "

For example: December has four numbers in it, which makes the percent 14.81. What I need is to multiply " ** " by 15 rather than 14. I am not sure how to round up... If anyone could give me some pointers. That would be much appreciated. The code above is used for every month to gather my "histogram".

Upvotes: 0

Views: 141

Answers (1)

Savir
Savir

Reputation: 18418

You should round using round:

>>> december="14.81"
>>> round(float(december))
 15.0 

If you want it to be an int, then use int.

>>> december="14.81"
>>> int(round(float(december)))
15

Fairly straightforward.

EDIT (as per the OP's comments to this answer):

Soooo... you're gonna have decemberNumber being 14.81 (or whatever... It doesn't matter: that's what variables are for)... and you want to show 15 (the closest int, rather) chunks of " ** " characters. If that's the case, the code below should work:

decemberHistogram=int(round(float(decemberNumber)))*" ** "

Upvotes: 1

Related Questions