numbers

JavaScript: Get the second digit from a number?

南楼画角 提交于 2019-12-02 21:47:05
I have a number assigned to a variable, like that: var myVar = 1234; Now I want to get the second digit (2 in this case) from that number without converting it to a string first. Is that possible? So you want to get the second digit from the decimal writing of a number. The simplest and most logical solution is to convert it to a string : var digit = (''+myVar)[1]; or var digit = myVar.toString()[1]; If you don't want to do it the easy way, or if you want a more efficient solution, you can do that : var l = Math.pow(10, Math.floor(Math.log(myVar)/Math.log(10))-1); var b = Math.floor(myVar/l);

Why does 00.0 cause a syntax error?

本小妞迷上赌 提交于 2019-12-02 21:00:19
This is weird. This is what happens at the JavaScript console in Chrome (version 42.0.2311.135, 64-bit). > 0 < 0 > 00 < 0 > 0.0 < 0 > 00.0 X Uncaught > SyntaxError: Unexpected number Firefox 37.0.2 does the same, although its error message is: SyntaxError: missing ; before statement There's probably some technical explanation regarding the way JavaScript parses numbers, and perhaps it can only happen when tinkering at the console prompt, but it still seems wrong. Why does it do that? The expressions 0.0 and 00.0 are parsed differently. 0.0 is parsed as a numeric literal 1 00.0 is parsed as: 00

What type would you map BigDecimal in Java/Hibernate in MySQL?

前提是你 提交于 2019-12-02 20:07:28
After going through the previous develop's trainwreck of code I realized I need to move all of the money based columns to not use floating point math. On the Java side this means using BigDecimal but when using Hibernate/JPA and MySQL 5 what would be the appropriate MySQL data type to make that column? Abimaran Kugathasan DECIMAL and NUMERIC. The recommended Java mapping for the DECIMAL and NUMERIC types is java.math.BigDecimal. The java.math.BigDecimal type provides math operations to allow BigDecimal types to be added, subtracted, multiplied, and divided with other BigDecimal types, with

Generate random number in a range [0, n) in C?

≯℡__Kan透↙ 提交于 2019-12-02 20:06:44
问题 Looking to make a really simple random number generator method in C. The numbers should be between 0 and 24 and can be for example 14.5f. Any help would be great, thanks! 回答1: float getRand() { float rnd = rand(); rnd /= RAND_MAX; return rnd * 24.0f; } Make sure you seed the random number generator with srand before use. 回答2: The Mersenne_twister is not only very simple, but also very strong. See the code on the link. However, if you can use GPL License, use the The GNU Scientific Library

Generating Even Random Numbers

一世执手 提交于 2019-12-02 19:58:58
问题 I need a code to generate only random EVEN numbers 2-100 There is tutorials on the web that generate random numbers but they're odd and even. Please understand i just need even numbers to generate. 回答1: This will work: NSInteger evenNumber = (arc4random_uniform(50) + 1) * 2; arc4random_uniform(50) will give results in the range 0 - 49. Adding 1 gives a value in the range 1 - 50. Multiplying by two gives you even numbers in the range 2 - 100. 回答2: 1, generate numbers 1-50 2, multiply all the

Why does double in C print fewer decimal digits than C++?

半城伤御伤魂 提交于 2019-12-02 19:54:39
I have this code in C where I've declared 0.1 as double. #include <stdio.h> int main() { double a = 0.1; printf("a is %0.56f\n", a); return 0; } This is what it prints, a is 0.10000000000000001000000000000000000000000000000000000000 Same code in C++, #include <iostream> using namespace std; int main() { double a = 0.1; printf("a is %0.56f\n", a); return 0; } This is what it prints, a is 0.1000000000000000055511151231257827021181583404541015625 What is the difference? When I read both are alloted 8 bytes? How does C++ print more numbers in the decimal places? Also, how can it go until 55

Get number of digits before decimal point

我的梦境 提交于 2019-12-02 19:54:39
I have a variable of decimal type and I want to check the number of digits before decimal point in it. What should I do? For example, 467.45 should return 3 . Solution without converting to string (which can be dangerous in case of exotic cultures): static int GetNumberOfDigits(decimal d) { decimal abs = Math.Abs(d); return abs < 1 ? 0 : (int)(Math.Log10(decimal.ToDouble(abs)) + 1); } Note, that this solution is valid for all decimal values UPDATE In fact this solution does not work with some big values, for example: 999999999999998 , 999999999999999 , 9999999999999939 ... Obviously, the

Oracle hibernate sequence generator problem

柔情痞子 提交于 2019-12-02 19:52:26
I am developing an application using oracle 11g, Java(struts2) and Hibernate. I have table named mytemp with column mytemp_id which is of type NUMBER(22,0). In my mytemp.hbm.xml file id is as given below <id name="mytempId" type="big_decimal"> <column name="MYTEMP_ID" precision="22" scale="0" /> <generator class="sequence"> <param name="sequence">MYTEMP_TEMP_ID_SEQ</param> </generator> </id> In my Oracle database sequence named "MYTEMP_TEMP_ID_SEQ" is created and working fine in Oracle. Now when I try to insert record using hibernate, it gives me following error org.hibernate.id

exiting a while loop with a negative integer

橙三吉。 提交于 2019-12-02 19:42:58
问题 I want to exit a while() when the user enters a negative number of any size. What kind of condition would I need at the start of the loop to get the loop to exit when the user enters a negative number? 回答1: Use an if condition to know the number is less than 0 or not. And if yes, just use break statement inside it, which will bring you out of the loop. Learn more about break statement from MSDN. 回答2: Well, what is a negative number? It's a number (call it x ) that is less than zero, or

Generating random numbers on different input texts and ensuring random numbers shown are unique

a 夏天 提交于 2019-12-02 19:04:41
问题 I'm trying to create a JavaScript code segment with JQuery which has random numbers that are constantly changing and will show in different text boxes. However, I can't figure out how to make the numbers show different values for each text box. How can I accomplish this? Here is my JavaScript code so far: var pcount = $(".pcount"); for(var i= 0; i < pcount.length; i++){ var n = []; var element = pcount.eq(i); setInterval(function() { n[i] = 10 + Math.floor(Math.random(Number($( ".pcount" )