floor

PHP关于数据的舍入归类:round、ceil、floor、number_format

怎甘沉沦 提交于 2019-12-10 03:36:26
下面是关于数字转化的几种方法: round -- 对浮点数进行四舍五入 例子: <?php echo round ( 3.4 ); // 3 echo round ( 3.6 ); // 4 echo round ( 3.6 , 0 ); // 4 echo round ( 1.95583 , 2 ); // 1.96 echo round ( 1241757 , - 3 ); // 1242000 echo round ( 5.055 , 2 ); // 5.06 ?> ceil -- 进一法取整 例子: <?php echo ceil ( 4.3 ); // 5 echo ceil ( 9.999 ); // 10 ?> floor -- 舍去法取整,跟ceil刚刚相反 例子: <?php echo floor ( 4.3 ); // 4 echo floor ( 9.999 ); // 9 ?> number_format -- 格式化数字为千分位 <?php $number = 1234; // english notation (default) $english_format_number = number_format($number); echo "First:".$english_format_number."<br />"; // French notation

php取整函数ceil,floor,round,intval函数的区别

夙愿已清 提交于 2019-12-09 20:37:18
开发过程中,遇到数据处理取整的时候,你会用哪个呢,小涛来介绍一下:PHP取整函数有ceil,floor,round,intval,下面详细介绍一下: 1、ceil — 进一法取整 说明 float ceil ( float $value ) 返回不小于 value 的下一个整数,value 如果有小数部分则进一位。ceil() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。 ceil() 例子 <?php echo ceil(4.3); // 5 echo ceil(9.999); // 10 ?> 2、floor — 舍去法取整 说明 float floor ( float $value ) 返回不大于 value 的下一个整数,将 value 的小数部分舍去取整。floor() 返回的类型仍然是 float,因为 float 值的范围通常比 integer 要大。 floor() 例子 <?php echo floor(4.3); // 4 echo floor(9.999); // 9 ?> 3、round — 对浮点数进行四舍五入 说明 float round ( float $val [, int $precision ] ) 返回将 val 根据指定精度 precision(十进制小数点后数字的数目)进行四舍五入的结果

How do I create a new Joda DateTime truncated to the last hour? [duplicate]

此生再无相见时 提交于 2019-12-09 05:00:44
问题 This question already has answers here : JodaTime equivalent of DateUtils.truncate() (4 answers) Closed 4 years ago . I am pulling timestamps from a file that I want to create a new DateTime for, but I want to create the DateTime at the floor of the hour (or any Joda Period will do). How Can I do this? 回答1: Wohoo, found it. Simple like everything in Joda once I traced down the calls. DateTime dt = new DateTime().hourOfDay().roundFloorCopy(); 来源: https://stackoverflow.com/questions/1207957/how

Write your own implementation of math's floor function, C

ぃ、小莉子 提交于 2019-12-08 18:46:55
问题 I was thinking about the floor function available in math.h . It is very easy to use it: #include <stdio.h> #include <math.h> int main(void) { for (double a = 12.5; a < 13.4; a += 0.1) printf("floor of %.1lf is %.1lf\n", a, floor(a)); return 0; } What if I would like to write my own implementation of it? Would it look simply like this: #include <stdio.h> #include <math.h> double my_floor(double num) { return (int)num; } int main(void) { double a; for (a = 12.5; a < 13.4; a += 0.1) printf(

Objective-C: floorf( ) returns wrong value

喜夏-厌秋 提交于 2019-12-08 00:39:06
问题 Here is the code: float passedPrice = 2.953; float placed = 1000.0; //3 decimals NSLog("%f", placed); // Gives 2953; float withNoFractions = floorf(passedPrice * placed); The value stored in withNoFractions is 2952! It shall be 2953. What is really strange is that it works some time. 回答1: Many decimal floating point fractions cannot be represented as exact fractions in binary, so they have to be approximated. 2.953 is being approximated as something like 2.95299999. When you multiply by 1000,

Is there a way to floor/ceil based on whether the value is over 0.5 or under?

为君一笑 提交于 2019-12-07 12:43:26
问题 I am trying to round my values so that if it's 0.5 or greater, it becomes 1 , else it becomes 0 . For example: 3.7 -> 4; 1.3 -> 1; 2.5 -> 3; ... Any ideas? 回答1: Math.Round(3.7,MidpointRounding.AwayFromZero); http://msdn.microsoft.com/en-us/library/system.midpointrounding.aspx In the above, I made use of AwayFromZero for rounding because the default is Banker's rounding, so if the fraction is 0.5, it is rounded to nearest even. So 3.5 becomes 4 (nearest even), but 2.5 becomes 2 (nearest even).

How to round down to the nearest power of 10?

亡梦爱人 提交于 2019-12-07 12:04:16
问题 I can't even find search keywords for this. Please consider this code: float inputValue = getInputValue(); float resultValue; if (inputValue < 0.1f) { resultValue = 0.01f; } else if (inputValue < 1.0f) { resultValue = 0.1f; } else if (inputValue < 10.0f) { resultValue = 1.0f; } else { resultValue = 10.0f; } and so on. There must be a more elegant way to do this. I guess the solution is easy, but I try to find a way now for 2 hours and read about round, ceil, floor...can't find it. Has anyone

将字符串转换为整数?

两盒软妹~` 提交于 2019-12-07 12:02:25
如何在JavaScript中将字符串转换为整数? #1楼 尝试parseInt。 var number = parseInt("10", 10); //number will have value of 10. #2楼 尝试parseInt函数: var number = parseInt("10"); 但有一个问题。 如果尝试使用parseInt函数转换“ 010”,它将检测为八进制数字,并返回数字8。因此,您需要指定一个基数(从2到36)。 在这种情况下,以10为底。 parseInt(string, radix) 例: var result = parseInt("010", 10) == 10; // Returns true var result = parseInt("010") == 10; // Returns false 请注意, parseInt 在解析任何有效内容后会忽略错误数据。 该GUID将解析为51: var result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true #3楼 最简单的方法是使用本机 Number 函数: var x = Number("1000") 如果这对您不起作用,则有 parseInt , 一元plus ,

将字符串转换为整数?

北战南征 提交于 2019-12-07 11:56:44
如何在JavaScript中将字符串转换为整数? #1楼 尝试parseInt。 var number = parseInt("10", 10); //number will have value of 10. #2楼 尝试parseInt函数: var number = parseInt("10"); 但有一个问题。 如果尝试使用parseInt函数转换“ 010”,它将检测为八进制数字,并返回数字8。因此,您需要指定一个基数(从2到36)。 在这种情况下,以10为底。 parseInt(string, radix) 例: var result = parseInt("010", 10) == 10; // Returns true var result = parseInt("010") == 10; // Returns false 请注意, parseInt 在解析任何有效内容后会忽略错误数据。 该GUID将解析为51: var result = parseInt('51e3daf6-b521-446a-9f5b-a1bb4d8bac36', 10) == 51; // Returns true #3楼 最简单的方法是使用本机 Number 函数: var x = Number("1000") 如果这对您不起作用,则有 parseInt , 一元plus ,

MySQL FLOOR function unexpected results

核能气质少年 提交于 2019-12-07 08:32:25
问题 CREATE TABLE table_name (col_a double(10,2), col_b double(10,2), col_c double(10,2)); INSERT INTO table_name VALUES(36.3, 0, 6.3); QUERY SELECT FLOOR(36.3- 0 -6.3), FLOOR(col_a - col_b - col_c) AS calc, col_a, col_b, col_c FROM table_name LIMIT 1; RESULT first selected value => FLOOR(36.3- 0 -6.3) result in 30 . second selected value => FLOOR(col_a - col_b - col_c) which is equals to FLOOR(36.3- 0 -6.3) result in 29 but i am expecting 30 Why these selects getting two different values? 回答1: