我需要一轮例如6.688689到6.7,但它总是显示我7.
我的方法:
Math.round(6.688689);
//or
Math.round(6.688689, 1);
//or
Math.round(6.688689, 2);
Run Code Online (Sandbox Code Playgroud)
但结果总是一样的7...我做错了什么?
考虑下面的四个百分比,表示为float数字:
13.626332%
47.989636%
9.596008%
28.788024%
-----------
100.000000%
Run Code Online (Sandbox Code Playgroud)
我需要将这些百分比表示为整数.如果我只是使用Math.round(),我最终总共有101%.
14 + 48 + 10 + 29 = 101
Run Code Online (Sandbox Code Playgroud)
如果我使用parseInt(),我最终总共有97%.
13 + 47 + 9 + 28 = 97
Run Code Online (Sandbox Code Playgroud)
什么是一个很好的算法来表示任意数量的百分比作为整数,同时仍然保持总计100%?
编辑:在阅读了一些评论和答案后,显然有很多方法可以解决这个问题.
在我看来,为了忠实于数字,"正确"的结果是最小化整体错误的结果,由相对于实际值引入的错误舍入量来定义:
value rounded error decision
----------------------------------------------------
13.626332 14 2.7% round up (14)
47.989636 48 0.0% round up (48)
9.596008 10 4.0% don't round up (9)
28.788024 29 2.7% round up (29)
Run Code Online (Sandbox Code Playgroud)
在平局(3.33,3.33,3.33)的情况下,可以做出任意决定(例如3,4,3).
我有问题四舍五入.我有一个浮点数,我想要舍入到十进制的十分之一.但是,我只能使用.round哪个基本上把它变成一个int,意思2.34.round # => 2. 是有一种简单的效果方式来做类似的事情2.3465 # => 2.35
一些python 3的功能和模块已被反向移植到python 2.7 python 3.1和python 2.7之间的显着差异是什么?
我在python中遇到了一个非常奇怪的问题.(使用python 2.4.x)
在Windows中:
>>> a = 2292.5
>>> print '%.0f' % a
2293
Run Code Online (Sandbox Code Playgroud)
但是在Solaris中:
>>> a = 2292.5
>>> print '%.0f' % a
2292
Run Code Online (Sandbox Code Playgroud)
但是在windows和solaris中都是一样的:
>>> a = 1.5
>>> print '%.0f' % a
2
Run Code Online (Sandbox Code Playgroud)
有人可以解释这种行为吗?我猜它的平台依赖于编译python的方式?
我有一个结果"1.444444",我想将此结果舍入到"1.5"这是我使用的代码:
a.text = String(round(13000 / 9000.0))
Run Code Online (Sandbox Code Playgroud)
但这是圆的"1.0",我需要圆到"1.5"
而这段代码:
a.text = String(ceil(13000 / 9000.0))
Run Code Online (Sandbox Code Playgroud)
圆到"2.0"
如何将Python Decimal实例舍入到特定的位数,同时舍入到最接近的小数?
我已尝试使用文档中.quantize(Decimal('.01'))概述的方法,并在之前的答案中提出,但尽管尝试了不同的ROUND_选项,但它似乎并没有正确地舍入.我也试过设置getcontext().prec,但这似乎只能控制整个数字中的总位数,而不仅仅是小数位数.
例如,我正在尝试做类似的事情:
assert Decimal('3.605').round(2) == Decimal('3.61')
assert Decimal('29342398479823.605').round(2) == Decimal('29342398479823.61')
assert Decimal('3.604').round(2) == Decimal('3.60')
assert Decimal('3.606').round(2) == Decimal('3.61')
Run Code Online (Sandbox Code Playgroud) 我知道如何在Python中四舍五入,这不是一个简单的技术问题。
我的问题是,从技术上讲,四舍五入将使一组百分比之和不等于100%。
例如:
a = 1
b = 14
Run Code Online (Sandbox Code Playgroud)
我想计算(a + b)中a和(a + b)中b的百分比。
答案应该是
a/(a + b) = 1/15
b/(a + b) = 14/15
Run Code Online (Sandbox Code Playgroud)
当我尝试四舍五入这些数字时,我得到了
1/15 = 6.66
14/15 = 93.33
Run Code Online (Sandbox Code Playgroud)
(我正在做地板),这使得这两个数字之和不等于100%。
在这种情况下,我们应该将1/15的上限设置为6.67,将14/15的上限设置为93.33。现在,它们加起来达到100%。在这种情况下,规则应为“四舍五入到最接近的数字”
但是,如果情况更复杂,请说3个数字:
a = 1
b = 7
c = 7
Run Code Online (Sandbox Code Playgroud)
地板:
1/15 = 6.66
7/15 = 46.66
7/15 = 46.66
Run Code Online (Sandbox Code Playgroud)
总计不等于100%。
天花板:
1/15 = 6.67
7/15 = 46.67
7/15 = 46.67
Run Code Online (Sandbox Code Playgroud)
总计不等于100%。
四舍五入(至最接近的数字)与上限相同。仍不等于100%。
所以我的问题是,在任何情况下我该怎么做以确保它们的总和为100%。
提前致谢。
更新:感谢您的评论提示。我从重复的帖子答案中获取了“最大余数”解决方案。
代码是:
def round_to_100_percent(number_set, digit_after_decimal=2):
"""
This function take a list …Run Code Online (Sandbox Code Playgroud) 我正试图将十进制的货币数量舍入到最接近的0.05.现在,我这样做:
def round_down(amount):
amount *= 100
amount = (amount - amount % 5) / Decimal(100)
return Decimal(amount)
def round_up(amount):
amount = int(math.ceil(float(100 * amount) / 5)) * 5 / Decimal(100)
return Decimal(amount)
Run Code Online (Sandbox Code Playgroud)
有没有办法,我可以更优雅地做到这一点,而无需使用python Decimals处理浮点数(也许使用量化)?
我已阅读以下页面:
python小数 - 使用ROUND_HALF_UP舍入到最接近的整数美元(无分钱)
http://docs.python.org/library/decimal.html
我有以下代码:
total_num = Decimal(str(total/10))
total_num.quantize(Decimal('1'), rounding=ROUND_UP)
Run Code Online (Sandbox Code Playgroud)
但它总是四舍五入?所以,如果我有221,我希望它返回23.现在我得到22.有什么我对此有误解吗?
[编辑]
我将其更改为以下内容: total_num = int(math.ceil(float(total)/10))
我需要一个int继续for循环有一个range.
任何人都可以帮忙吗?我收到标题中的错误
bool fexists(const char *filename)
{
std::ifstream ifile(filename);
return ifile;
}
Run Code Online (Sandbox Code Playgroud) 如何将 a 更改long为char8 字节的数组以及如何将char8 字节的数组更改为long?
#include <iostream>
using namespace std;
int main () {
long num=33;
char* z;
z = new char[8];
z = reinterpret_cast<char *>(&num); // long to char array
long r;
r = reinterpret_cast<long>(&z); // char array to long
cout << r << "\n"; // why is the output not 33
printf("%x ", z[0]); //print the 8 Byte char array
printf("%x ", z[1]);
printf("%x ", z[2]);
printf("%x ", z[3]);
printf("%x …Run Code Online (Sandbox Code Playgroud)