Как вы округлите число в Python?

Это то, что я использую для расширения функциональности WebClient. StatusCode и StatusDescription всегда будут содержать самый последний код ответа / описание.

                /// <summary>
                /// An expanded web client that allows certificate auth and 
                /// the retrieval of status' for successful requests
                /// </summary>
                public class WebClientCert : WebClient
                {
                    private X509Certificate2 _cert;
                    public WebClientCert(X509Certificate2 cert) : base() { _cert = cert; }
                    protected override WebRequest GetWebRequest(Uri address)
                    {
                        HttpWebRequest request = (HttpWebRequest)base.GetWebRequest(address);
                        if (_cert != null) { request.ClientCertificates.Add(_cert); }
                        return request;
                    }
                    protected override WebResponse GetWebResponse(WebRequest request)
                    {
                        WebResponse response = null;
                        response = base.GetWebResponse(request);
                        HttpWebResponse baseResponse = response as HttpWebResponse;
                        StatusCode = baseResponse.StatusCode;
                        StatusDescription = baseResponse.StatusDescription;
                        return response;
                    }
                    /// <summary>
                    /// The most recent response statusCode
                    /// </summary>
                    public HttpStatusCode StatusCode { get; set; }
                    /// <summary>
                    /// The most recent response statusDescription
                    /// </summary>
                    public string StatusDescription { get; set; }
                }

Таким образом, вы можете сделать сообщение и получить результат через:

            byte[] response = null;
            using (WebClientCert client = new WebClientCert())
            {
                response = client.UploadValues(postUri, PostFields);
                HttpStatusCode code = client.StatusCode;
                string description = client.StatusDescription;
                //Use this information
            }
414
задан Mr. Me 24 February 2018 в 04:51
поделиться

3 ответа

Функция ceil (потолок):

import math
print(math.ceil(4.2))
781
ответ дан 22 November 2019 в 23:11
поделиться

https://docs.python.org/2/library/math.html ищет math.ceil

, "Возвращают потолок x как плавание, самое маленькое целочисленное значение, больше, чем или равный x."

-2
ответ дан 22 November 2019 в 23:11
поделиться

Используйте math.ceil для округления в большую сторону:

>>> import math
>>> math.ceil(5.4)
6.0

ПРИМЕЧАНИЕ : ввод должен быть с плавающей точкой.

Если вам нужно целое число, вызовите int , чтобы преобразовать его:

>>> int(math.ceil(5.4))
6

Кстати, используйте math.floor , чтобы округлить вниз и округлить для округления до ближайшего целого числа.

>>> math.floor(4.4), math.floor(4.5), math.floor(5.4), math.floor(5.5)
(4.0, 4.0, 5.0, 5.0)
>>> round(4.4), round(4.5), round(5.4), round(5.5)
(4.0, 5.0, 5.0, 6.0)
>>> math.ceil(4.4), math.ceil(4.5), math.ceil(5.4), math.ceil(5.5)
(5.0, 5.0, 6.0, 6.0)
27
ответ дан 22 November 2019 в 23:11
поделиться
Другие вопросы по тегам:

Похожие вопросы: