numbers

How to convert a negative number to positive?

血红的双手。 提交于 2019-11-27 11:59:36
How can I convert a negative number to positive in Python? (And keep a positive one.) >>> n = -42 >>> -n # if you know n is negative 42 >>> abs(n) # for any n 42 Don't forget to check the docs . simply multiplying by -1 works in both ways ... >>> -10 * -1 10 >>> 10 * -1 -10 BoltClock If "keep a positive one" means you want a positive number to stay positive, but also convert a negative number to positive, use abs() : >>> abs(-1) 1 >>> abs(1) 1 The inbuilt function abs() would do the trick. positivenum = abs(negativenum) Tauquir In [6]: x = -2 In [7]: x Out[7]: -2 In [8]: abs(x) Out[8]: 2

HTML to Excel: How can tell Excel to treat columns as numbers?

╄→尐↘猪︶ㄣ 提交于 2019-11-27 11:36:20
I need to achieve the following when opening an HTML in Excel (Response.contentType="application/vnd.ms-excel") : force Excel to consider content of td cells as numbers make the above so that any subsequent user-entered formulas work on these cells (when the spreadsheet is opened) So far I was successful with adding style="vnd.ms-excel.numberformat:0.00" to the td cells in question. The contents of the cells are correctly shown as numbers when I right click on them in the Excel, however the formulas don't work. If successful, that technique would be quite useful because any web Excel report

Javascript - validation, numbers only

£可爱£侵袭症+ 提交于 2019-11-27 11:34:51
问题 I'm trying to get my login form to only validate if only numbers were inputted. I can it to work if the input is only digits, but when i type any characters after a number, it will still validate etc. 12akf will work. 1am will work. How can i get past this? Part of the Login <form name="myForm"> <label for="firstname">Age: </label> <input name="num" type="text" id="username" size="1"> <input type="submit" value="Login" onclick="return validateForm()"> function validateForm() { var z =

Computing target number from numbers in a set

陌路散爱 提交于 2019-11-27 11:28:12
问题 I'm working on a homework problem that asks me this: Tiven a finite set of numbers, and a target number, find if the set can be used to calculate the target number using basic math operations (add, sub, mult, div) and using each number in the set exactly once (so I need to exhaust the set). This has to be done with recursion. So, for example, if I have the set {1, 2, 3, 4} and target 10, then I could get to it by using ((3 * 4) - 2)/1 = 10. I'm trying to phrase the algorithm in pseudo-code,

Convert long number into abbreviated string in JavaScript, with a special shortness requirement

喜夏-厌秋 提交于 2019-11-27 11:25:35
In JavaScript, how would one write a function that converts a given [edit: positive integer ] number (below 100 billion) into a 3-letter abbreviation -- where 0-9 and a-z/A-Z are counting as a letter, but the dot (as it's so tiny in many proportional fonts) would not, and would be ignored in terms of the letter limit? This question is related to this helpful thread , but it's not the same; for instance, where that function would turn e.g. "123456 -> 1.23k" ("123.5k" being 5 letters) I am looking for something that does "123456 -> 0.1m" ("0[.]1m" being 3 letters). For instance, this would be

jquery how to empty input field

*爱你&永不变心* 提交于 2019-11-27 10:45:44
I am in a mobile app and I use an input field in order user submit a number. When I go back and return to the page that input field present the latest number input displayed at the input field. Is there any way to clear the field every time the page load? $('#shares').keyup(function(){ payment = 0; calcTotal(); gtotal = ($('#shares').val() * 1) + payment; gtotal = gtotal.toFixed(2); $("p.total").html("Total Payment: <strong>" + gtotal + "</strong>"); }); You can clear the input field by using $('#shares').val(''); $(document).ready(function(){ $('#shares').val(''); }); Setting val('') will

Convert boolean result into number/integer

陌路散爱 提交于 2019-11-27 10:34:45
I have a variable that stores false or true , but I need 0 or 1 instead, respectively. How can I do this? Javascript has a ternary operator you could use: var i = result ? 1 : 0; Use the unary + operator , which converts its operand into a number. + true; // 1 + false; // 0 Note, of course, that you should still sanitise the data on the server side, because a user can send any data to your sever, no matter what the client-side code says. Imho the best solution is: fooBar | 0 This is used in asm.js to force integer type. I prefer to use the Number function . It takes an object and converts it

Generating non-repeating random numbers in Python

♀尐吖头ヾ 提交于 2019-11-27 10:32:00
Ok this is one of those trickier than it sounds questions so I'm turning to stack overflow because I can't think of a good answer. Here is what I want: I need Python to generate a simple a list of numbers from 0 to 1,000,000,000 in random order to be used for serial numbers (using a random number so that you can't tell how many have been assigned or do timing attacks as easily, i.e. guessing the next one that will come up). These numbers are stored in a database table (indexed) along with the information linked to them. The program generating them doesn't run forever so it can't rely on

LeetCode167-两数之和II-输入有序数组

六眼飞鱼酱① 提交于 2019-11-27 10:20:40
【题目】 给定一个已按照升序排列 的有序数组,找到两个数使得它们相加之和等于目标数。 函数应该返回这两个下标值 index1 和 index2,其中 index1 必须小于 index2。 说明: 返回的下标值(index1 和 index2)不是从零开始的。 你可以假设每个输入只对应唯一的答案,而且你不可以重复使用相同的元素。 示例: 输入: numbers = [2, 7, 11, 15], target = 9 输出: [1,2] 解释: 2 与 7 之和等于目标数 9 。因此 index1 = 1, index2 = 2 。 来源:力扣(LeetCode) 链接:https://leetcode-cn.com/problems/two-sum-ii-input-array-is-sorted 著作权归领扣网络所有。商业转载请联系官方授权,非商业转载请注明出处。 【思路】 利用有序数组的特性,使用双指针方法。 初始化两个指针分别指向数组第一个和最后一个元素,比较两者之和与目标值的大小, (1)大于目标值,右指针--; (2)小于目标值,左指针++; (3)等于目标值,返回。 【C++代码】 class Solution { public: vector<int> twoSum(vector<int>& numbers, int target) { vector<int> b

Prevent negative inputs in form input type=“number”?

旧时模样 提交于 2019-11-27 10:18:55
问题 I want to restrict user input to positive numbers in an html form. I know you can set min="0", however it is possible to bypass this by manually entering a negative number. Is there any other way to solve this without writing a validation function? 回答1: This uses Javascript, but you don't have to write your own validation routine. Instead just check the validity.valid property. This will be true if and only if the input falls within the range. <html> <body> <form action="#"> <input type=