我正在尝试检查变量内的数字的十进制数是否大于50或小于50。然后根据其是否大于或等于50,将十进制数四舍五入为99,如果较小则将其四舍五入为00 。
这是我的一段代码:
public function roundPrice($price)
{
return round( (ceil($price) - 0.01), 2);
}
Run Code Online (Sandbox Code Playgroud)
它使所有十进制数舍入到99。我只需要将50或更高的小数点四舍五入到99,而49或更少的小数则变为00。
我如何在PHP中实现呢?非常感谢,我被困在这里,不知道怎么做。
我正在寻找一个向上或向下舍入的Java函数,但通常情况下,如3.2到3,以及3.6到4.有很多答案,但所有这些答案都要么专门整理,要么四舍五入.如果没有,那么我想我将不得不创建它,但只是想看看是否有其他人想要相同并找到一个.提前致谢!
所以,显然PHP round()
将结果输出为根据当前语言环境设置格式化的字符串.round( 10000.326, 1 )
可能会返回"1.000,3",如果您打算立即显示结果,这很好,但如果您计划进一步使用它,那就没那么好.
php.net讨论暗示没有办法停止round()
本地化输出.库中是否真的没有"纯"舍入函数返回int或float/double,以便结果可以用于算术运算,或创建自己的唯一选项?
在C++中是否有任何命令可以制作,
1.354322e-23
Run Code Online (Sandbox Code Playgroud)
成
0
Run Code Online (Sandbox Code Playgroud)
这是我的(简单)程序
#include "stdafx.h"
#include <iostream>
#include<iomanip>
int main()
{
float x;
std::cin >> x;
std::cout << x << std::endl;
return 0;
}
Run Code Online (Sandbox Code Playgroud)
当我输入值时,
2.2356e-17
Run Code Online (Sandbox Code Playgroud)
它给,
2.2356e-017
Run Code Online (Sandbox Code Playgroud)
std::setprecision
不会工作......
编辑: 好的,这是我的问题.我创建了一个程序,可以给出sin cos和tan值.
对于cos 90,我希望它为0而不是-4.311e-008
继承人我的真实计划
#include "stdafx.h"
#include <iostream>
#include<iomanip>
float Pi()
{
float pi = (atan(1) * 4);
return pi;
}
int Selector()
{
using namespace std;
cout << "Type:\t1 for Degrees\n\t2 for Radians\n\t3 for Gradians\n\nYour Choice : ";
int x; …
Run Code Online (Sandbox Code Playgroud) 简单的操作,例如舍入数字,创建增加的向量等,在python中无法可靠地完成(参见下面的示例).python社区给出的常见解释是浮点精度.
Python 2.7.11 (default, Sep 29 2016, 13:33:00)
[GCC 5.3.1 20160406 (Red Hat 5.3.1-6)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import numpy
>>> numpy.round(2.5)
2.0
>>> numpy.round(3.5)
4.0
>>> numpy.arange(89,90,0.1)
array([ 89. , 89.1, 89.2, 89.3, 89.4, 89.5, 89.6, 89.7, 89.8, 89.9])
>>> numpy.arange(89+0.1,90,0.1)
array([ 89.1, 89.2, 89.3, 89.4, 89.5, 89.6, 89.7, 89.8, 89.9, 90. ])
>>> numpy.arange(19,20,0.1)
array([ 19. , 19.1, 19.2, 19.3, 19.4, 19.5, 19.6, 19.7, 19.8, 19.9])
>>> numpy.arange(19+0.1,20,0.1)
array([ 19.1, …
Run Code Online (Sandbox Code Playgroud) 可能重复:
在PHP中舍入到最接近的50,000
我有这个,52.52,我希望它变成52.5.我怎么用PHP做到这一点?我尝试了round()但没有奏效.谢谢.