digits

Function to count number of digits in string

和自甴很熟 提交于 2019-11-29 05:28:27
问题 I was looking for a quick PHP function that, given a string, would count the number of numerical characters (i.e. digits) in that string. I couldn't find one, is there a function to do this? 回答1: This can easily be accomplished with a regular expression. function countDigits( $str ) { return preg_match_all( "/[0-9]/", $str ); } The function will return the amount of times the pattern was found, which in this case is any digit. 回答2: first split your string, next filter the result to only

How do I generate a random n digit integer in Java using the BigInteger class?

妖精的绣舞 提交于 2019-11-29 03:34:57
I am unsure about how to generate a random n digit integer in Java using the BigInteger class. private static Random rnd = new Random(); public static String getRandomNumber(int digCount) { StringBuilder sb = new StringBuilder(digCount); for(int i=0; i < digCount; i++) sb.append((char)('0' + rnd.nextInt(10))); return sb.toString(); } And then you can use it: new BigInteger(getRandomNumber(10000)) Carl According to the docs, there is a constructor to do what you want in java 6: BigInteger(int, java.util.Random) To that, you need only add a randomly selected 5000th digit-i.e. Use the rng

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

为君一笑 提交于 2019-11-29 02:59:29
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? int value = 123; while (value > 0) { int digit = value % 10; // do something with digit value /= 10; } 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; //for example unsigned int dig=count(num); char arr[dig]; while (dig--) { arr[dig]=num%10; num/=10; } As a hint,

Finding the number of digits of an integer

不打扰是莪最后的温柔 提交于 2019-11-28 16:19:02
What is the best method to find the number of digits of a positive integer? I have found this 3 basic methods: conversion to string String s = new Integer(t).toString(); int len = s.length(); for loop for(long long int temp = number; temp >= 1;) { temp/=10; decimalPlaces++; } logaritmic calculation digits = floor( log10( number ) ) + 1; where you can calculate log10(x) = ln(x) / ln(10) in most languages. First I thought the string method is the dirtiest one but the more I think about it the more I think it's the fastest way. Or is it? Mike Dunlavey There's always this method: n = 1; if ( i >=

Get number of digits with JavaScript

微笑、不失礼 提交于 2019-11-28 15:30:00
As the title of my post suggests, I would like to know how many digits var number has. For example: If number = 15; my function should return 2 . Currently, it looks like this: function getlength(number) { return number.toString().length(); } But Safari says it is not working due to a TypeError : '2' is not a function (evaluating 'number.toString().length()') As you can see, '2' is actually the right solution. But why is it not a function ? VisioN length is a property, not a method. You can't call it, hence you don't need parenthesis () : function getlength(number) { return number.toString()

C++ - how to find the length of an integer

血红的双手。 提交于 2019-11-28 09:09:16
I'm trying to find a way to find the length of an integer (number of digits) and then place it in an integer array. The assignment also calls for doing this without the use of classes from the STL, although the program spec does say we can use "common C libraries" (gonna ask my professor if I can use cmath, because I'm assuming log10(num) + 1 is the easiest way, but I was wondering if there was another way). Ah, and this doesn't have to handle negative numbers. Solely non-negative numbers. I'm attempting to create a variant "MyInt" class that can handle a wider range of values using a dynamic

Given a double, need to find how many digits in total

◇◆丶佛笑我妖孽 提交于 2019-11-28 08:39:06
问题 I have a double which is not necessarily positive but usually. It can be 0.xxxx000 or X.xxxx00000 or XX.00000 or 0.xxx0xxx00000, where eventually there are all 0's to the right of the last number. I need to keep track of how many digits there are. I've been having trouble with this, any help? This is C. 回答1: Use sprintf to turn it into a string and do whatever counting/testing you need to do on the digits 回答2: A double has 52 mantissa bits plus an implicit "1" bit, so you should be able to

Android : How to set acceptable numbers and characters in EditText?

六月ゝ 毕业季﹏ 提交于 2019-11-28 06:57:27
I have to set acceptable characters "0123456789" and "semicolon" in the EditText. Below is the code I'm using. android:digits="0123456789;" android:inputType="number|text The problem with that implementation is in HTC phones, semicolon can't be entered but in Samsung and Sony Ericsson, semicolon can be entered. Another problem is when I entered semicolon in Samsung and Sony Ericsson, semicolon can't be deleted. Is there any missing property in the above code? Thanks in advance. Shankar Agarwal Android provides an easy way to edit text fields by modifying the layout xml and adding an android

JavaScript format number to day with always 3 digits [duplicate]

删除回忆录丶 提交于 2019-11-28 00:02:29
Possible Duplicate: How can I create a Zerofilled value using JavaScript? I have to output a day number that must always have 3 digits. Instead of 3 it must write 003 , instead of 12 it must write 012 . If it is greater than 100 output it without formatting. I wonder if there's a regex that I could use or some quick in-line script, or I must create a function that should do that and return the result. Thanks! How about: zeroFilled = ('000' + x).substr(-3) For arbitrary width: zeroFilled = (new Array(width).join('0') + x).substr(-width) As per comments, this seems more accurate: lpad = function

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; /