Questions tagged [digits]

Questions dealing with getting, manipulating, or changing the digits of numbers

Use this tag for questions dealing with getting, manipulating, or changing the digits of numbers. More about digits on wikipedia

1098 questions
1458
votes
39 answers

How to round a number to n decimal places in Java

What I would like is a method to convert a double to a string which rounds using the half-up method - i.e. if the decimal to be rounded is 5, it always rounds up to the next number. This is the standard method of rounding most people expect in most…
Alex Spurling
  • 54,094
  • 23
  • 70
  • 76
515
votes
15 answers

Check if string contains only digits

I want to check if a string contains only digits. I used this: var isANumber = isNaN(theValue) === false; if (isANumber){ .. } But realized that it also allows + and -. Basically, I want to make sure an input contains ONLY digits and no other…
patad
  • 9,364
  • 11
  • 38
  • 44
237
votes
39 answers

Given a number, find the next higher number which has the exact same set of digits as the original number

I just bombed an interview and made pretty much zero progress on my interview question. Given a number, find the next higher number which has the exact same set of digits as the original number. For example: given 38276 return 38627 I wanted to…
bhan
  • 2,489
  • 3
  • 19
  • 26
182
votes
20 answers

Get number of digits with JavaScript

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…
bit4fox
  • 2,021
  • 2
  • 13
  • 8
167
votes
33 answers

Efficient way to determine number of digits in an integer

What is a very efficient way of determining how many digits there are in an integer in C++?
Seth
  • 8,213
  • 14
  • 71
  • 103
147
votes
4 answers

Controlling number of decimal digits in print output in R

There is an option in R to get control over digit display. For example: options(digits=10) is supposed to give the calculation results in 10 digits till the end of R session. In the help file of R, the definition for digits parameter is as…
Mehper C. Palavuzlar
  • 10,089
  • 23
  • 56
  • 69
137
votes
12 answers

How to tell if string starts with a number with Python?

I have a string that starts with a number (from 0-9) I know I can "or" 10 test cases using startswith() but there is probably a neater solution so instead of writing if (string.startswith('0') || string.startswith('2') || string.startswith('3')…
Illusionist
  • 5,204
  • 11
  • 46
  • 76
106
votes
11 answers

Sum the digits of a number

If I want to find the sum of the digits of a number, i.e.: Input: 932 Output: 14, which is (9 + 3 + 2) What is the fastest way of doing this? I instinctively did: sum(int(digit) for digit in str(number)) and I found this online: sum(map(int,…
SpFW
  • 1,099
  • 2
  • 8
  • 5
97
votes
11 answers

Which is best data type for phone number in MySQL and what should Java type mapping for it be?

I am using MySQL with the Spring JDBC template for my web application. I need to store phone numbers with only digits (10). I am a little bit confused about data type using data type. What is the preferable data type for it in MySQL? What should be…
Vishal Zanzrukia
  • 4,902
  • 4
  • 38
  • 82
95
votes
11 answers

in python how do I convert a single digit number into a double digits string?

So say i have a = 5 i want to print it as a string '05'
Joe Schmoe
  • 1,815
  • 3
  • 15
  • 14
80
votes
2 answers

Show a leading zero if a number is less than 10

Possible Duplicate: JavaScript equivalent to printf/string.format How can I create a Zerofilled value using JavaScript? I have a number in a variable: var number = 5; I need that number to be output as 05: alert(number); // I want the alert to…
Nicekiwi
  • 4,567
  • 11
  • 49
  • 88
79
votes
25 answers

How do I separate an integer into separate digits in an array in JavaScript?

This is my code so far: var n = 123456789; var d = n.toString().length; var digits = []; var squaredDigits = []; for (i = d; i >= 1; i--) { var j = k / 10; var r = (n % k / j) - 0.5; var k = Math.pow(10, i); var result = r.toFixed();…
magnusbl
  • 799
  • 1
  • 6
  • 5
65
votes
19 answers

Finding the number of digits of an integer

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 >=…
daniel.sedlacek
  • 8,129
  • 9
  • 46
  • 77
49
votes
12 answers

Gets last digit of a number

I need to define the last digit of a number assign this to value. After this, return the last digit. My snippet of code doesn't work correctly... Code: public int lastDigit(int number) { String temp = Integer.toString(number); int[] guess =…
catch23
  • 17,519
  • 42
  • 144
  • 217
40
votes
11 answers

Delete digits in Python (Regex)

I'm trying to delete all digits from a string. However the next code deletes as well digits contained in any word, and obviously I don't want that. I've been trying many regular expressions with no success. Thanks! s = "This must not b3 delet3d,…
Menda
  • 1,783
  • 2
  • 14
  • 20
1
2 3
73 74