floor

将字符串转换为整数?

廉价感情. 提交于 2019-12-06 20:21:24
如何在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的常用内置函数

橙三吉。 提交于 2019-12-06 13:41:31
1.数学类 floor(x) 返回小宇等于x的最小整数 floor(1.5) 返回结果是1 rand() 返回0-1的随机数 取100内的随机数 floor(100*rand()) round(x) 取离x的最近整数(和floor有点类似) 应用场景: 给一组学生生成一个随机的100以内的分数 set score =floor(rand()*100); 字符串类 length(char) 返回字符串的长度 upper(char) 把字符串全部大写 lower(char) 把字符串全部小写 left(char,n) 返回char的左边n个字符(right相反) trim(s) 去掉字符串s开始和结尾处的空格 reverse(s) 将字符串反转 concat("a","b","c") 连接abc 从用户表将姓名全部大写后输出 select upper(name) from user; 读取文件(mysql里读取其他地方的文件) select load_file('/home/test.txt'); 日期时间类 curdate() curtime() 当前日期和当前时间 NOW(),CURRENT_TIMESTAMP() 当前日期和时间组合在一起 UNIX_TIMESTAMP() 以UNIX时间戳的形式返回当前时间 这个一般比较常用,在组合查找的时候 (UNIX_TIMESTAMP(d

Objective-C: floorf( ) returns wrong value

柔情痞子 提交于 2019-12-06 08:23:58
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. 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, the result is 2952.99999, and when you get the floor of this, it's 2952. To solve this, you can either use

6. Scala模式匹配

China☆狼群 提交于 2019-12-05 23:00:16
模式匹配 Java中 case when:对一个值进行条件判断,返回针对不同的条件进行不同的处理 Scala语法: 变量 match{ case value1 => 代码一 case vlaue2 => 代码二 ... case _ => 代码N } package com.yy.scala object MatchApp extends App { matchName("Tom", 60) matchName("Lucy", 60) matchArray(Array("Tom","Lily")) matchList(List("Tom","Lily","Lucy")) matchType(1) matchType(Map("age"->12)) caseclassMath(CTO("David", "10")) caseclassMath(Employee("Kit", "14")) //基础类型匹配 def matchName(name: String, age : Int): Unit ={ name match { case "Tom" => println("汤姆") case "Jim" => println("吉姆") case _ if age > 50 => println("old "+name) case _ => println("young "+name)

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

余生颓废 提交于 2019-12-05 22:00:17
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? 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). So you choose a different method as shown above to make 3.5 to 4 and 2.5 to 3. The simplest way is add 0.5

MySQL FLOOR function unexpected results

风格不统一 提交于 2019-12-05 13:07:45
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? This is a known problem in MySQL when using the double or float type, which are not stored internally exactly

JS基础入门篇(十二)—JSON和Math

对着背影说爱祢 提交于 2019-12-05 09:28:25
1.JSON JSON: 对象格式的字符串 轻量的数据传输格式 注意事项: 键名 需要 使用 双引号 包起来 JOSN有两个方法:JSON.parse和 JSON.stringify。 JSON.parse,将后台传来的字符串转化为对象。其字符串的内容就是对象才需要这个转。 JSON.stringify,将后台传来的对象转化为字符串。 <script> var book = '{"title": "Harry Potter","author": "J K. Rowling","year": 2005,"price": 29.99}'; console.log( JSON.parse( book ) ); console.log( book ); //------------------------------------------ var obj = { name: "k", age: 25 }; console.log( JSON.stringify( obj ) ); console.log( obj ); </script> 结果为。 2.Math — Part1 Math.ceil 对数向上取整 Math.floor 对数向下取整 Math.random 取0到1的随机数。包括0,但不包括1。 Math.abs 取绝对值 举例说明: <script> console

Why does Binary search algorithm use floor and not ceiling - not in an half open range

亡梦爱人 提交于 2019-12-05 07:34:37
When we have an array with indexes from 0 to n for example. when I use the Binary search using floor or ceiling when calculating the middle index I get the same out put. int middle = ceiling ((left+right)/2); Is there a reason using floor over ceiling ? what bug will happen using the ceiling ? There's no difference in complexity. Both variants are log(n). Depending on the rest of your implementation, you may get a different result if your array looks for instance like 0 1 1 1 1 2 and looking for the index of a 1 . This all depends on how you update your left and right variable. Normally, we

Math.floor(Math.random() * array.length)

走远了吗. 提交于 2019-12-04 23:19:27
Math.floor(Math.random() * array.length) 返回长度内的索引 eg: changeLimit () {   function getArrayItems(arr, num) {     const temp_array = [];     for(let index in arr) {       temp_array.push(arr[index]);     }     const return_array = [];     for (let i = 0; i<num; i++) {       if(temp_array.length>0) {         const arrIndex = Math.floor(Math.random()*temp_array.length);         return_array[i] = temp_array[arrIndex];         temp_array.splice(arrIndex, 1);       } else {         break;       }     }     return return_array;   }   this.randomMovieList = getArrayItems(this.movieList, 5); } 来源: https