numbers

Javascript function need allow numbers, dot and comma

不羁岁月 提交于 2019-11-27 17:55:07
问题 I need to create a function to allow numbers dot and comma. So anyone corrects the following function function on(evt) { var theEvent = evt || window.event; var key = theEvent.keyCode || theEvent.which; key = String.fromCharCode( key ); var regex = /[0-9]|\./; if( !regex.test(key) ) { theEvent.returnValue = false; if(theEvent.preventDefault) theEvent.preventDefault(); } } 回答1: Firstly your regex currently doesn't allow comma, which is your requirement. Secondly, you haven't used any

Negative numbers to binary string in JavaScript

依然范特西╮ 提交于 2019-11-27 17:26:55
Anyone knows why javascript Number.toString function does not represents negative numbers correctly? //If you try (-3).toString(2); //shows "-11" // but if you fake a bit shift operation it works as expected (-3 >>> 0).toString(2); // print "11111111111111111111111111111101" I am really curious why it doesn't work properly or what is the reason it works this way? I've searched it but didn't find anything that helps. Short answer: The toString() function takes the decimal, converts it to binary and adds a "-" sign. A zero fill right shift converts it's operands to signed 32-bit integers in two

What's the deal with char.GetNumericValue?

ε祈祈猫儿з 提交于 2019-11-27 17:26:30
问题 I was working on Project Euler 40, and was a bit bothered that there was no int.Parse(char) . Not a big deal, but I did some asking around and someone suggested char.GetNumericValue . GetNumericValue seems like a very odd method to me: Takes in a char as a parameter and returns...a double? Returns -1.0 if the char is not '0' through '9' So what's the reasoning behind this method, and what purpose does returning a double serve? I even fired up Reflector and looked at InternalGetNumericValue,

Convert css width string to regular number

帅比萌擦擦* 提交于 2019-11-27 17:21:13
问题 While trying to compute the width of an hidden element I found that jquery.width() returns 0 for that elements' width. I found out that using jquery.css('width') would return the correct width by using the declared style width (even if that value is different from the initial stylesheet). The problem is that the css('width') method returns a string, generally in a "100px" fashion. My question resolves into: how to retrieve the number from the "100px" string? Is there an easy way? 回答1: If it

C: how to break apart a multi digit number into separate variables?

試著忘記壹切 提交于 2019-11-27 17:14:46
问题 Say I have a multi-digit integer in C. I want to break it up into single-digit integers. 123 would turn into 1 , 2 , and 3 . How can I do this, especially if I don't know how many digits the integer has? 回答1: int value = 123; while (value > 0) { int digit = value % 10; // do something with digit value /= 10; } 回答2: First, count the digits: unsigned int count(unsigned int i) { unsigned int ret=1; while (i/=10) ret++; return ret; } Then, you can store them in an array: unsigned int num=123; /

Set a font specifically for all numbers on the page

£可爱£侵袭症+ 提交于 2019-11-27 16:20:39
问题 For my webpage, I chose a font that works well for all the letters. However, for all numbers I'd like to use a different font. Is there a way that I can set a CSS rule to target all the numbers on the page? If I can't do it strictly with CSS, my first thought is to use regular expressions to surround all numbers with a span with a "numbers" class and apply a font for that class. Is there a better way to do this? 回答1: You can do it in JavaScript relatively simply by traversing the document so

Is there a way in Matlab using the pseudo number generator to generate numbers within a specific range?

末鹿安然 提交于 2019-11-27 16:13:41
For example: round(7*rand(1,5)) Generates 5 numbers between 1 and 7 Is there a way to generate 5 random numbers between 5 and 7? Or an abstraction of that? More generally: minInt = 5; maxInt = 7; numInts = 10; r = randi([minInt, maxInt],[1,numInts]) r = 6 7 7 7 6 5 5 5 7 5 gnovice First, if you are wanting to generate random integer values, it's better to use the function RANDI . Then it's just a matter of shifting and scaling the random numbers accordingly. The following should give you random integers between 5 and 7 inclusive: nums = randi(3,[1 5])+4; EDIT: As Amro's comment and Doug's

json_decode AND json_encode long integers without loosing data

旧时模样 提交于 2019-11-27 16:10:29
As noted in the PHP documentation, when json_decode ing a data structure containing long integers, they'll be converted to floats. The workaround is to use JSON_BIGINT_AS_STRING , which preserves them as strings instead. When json_encode ing such values, JSON_NUMERIC_CHECK will encode those numbers back into large integers: $json = '{"foo":283675428357628352}'; $obj = json_decode($json, false, JSON_BIGINT_AS_STRING); $json2 = json_encode($obj, JSON_NUMERIC_CHECK); var_dump($json === $json2); // true Using this method for a correct roundtrip of the data is prone to errors. If a property

It's possible in Swing configure a JTextField to only accept numbers? [duplicate]

↘锁芯ラ 提交于 2019-11-27 15:58:32
Possible Duplicate: Is there any way to accept only numeric values in a JTextField? There any functionality on the JTextField of Swing that allow only positive numbers inside a range of numbers? Example: I can only enter numbers between 10 and 30. Numbers out of this range will not even appear in the field. Use a JSpinner with a SpinnerNumberModel - the end user will thank you. OK not literally, but at least they will not curse your name for forcing them to type in something they'd like to choose using the arrow keys. E.G. import javax.swing.*; class NumberChooser { public static void main

Why int j = 012 giving output 10?

风格不统一 提交于 2019-11-27 15:34:07
In my actual project It happened accidentally here is my modified small program. I can't figure out why it is giving output 10 ? public class Int { public static void main(String args[]) { int j=012;//accidentaly i put zero System.out.println(j);// prints 10?? } } After that, I put two zeros still giving output 10. Then I change 012 to 0123 and now it is giving output 83? Can anyone explain why? Abimaran Kugathasan Than I change 012 to 0123 and now it is giving output 83? Because, it's taken as octal base (8), since that numeral have 0 in leading. So, it's corresponding decimal value is 10 012