digits

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

試著忘記壹切 提交于 2019-11-27 02:45:17
问题 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

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

本小妞迷上赌 提交于 2019-11-26 23:21:45
问题 This question already has answers here : Closed 7 years ago . 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! 回答1: How about:

Identify the digits in a given number.

此生再无相见时 提交于 2019-11-26 22:06:38
问题 I'm new to programming, and I'm stuck at a problem. I want my program to identify the separate digits in a given number, like if I input 4692 , it should identify the digits and print 4 6 9 2 . And yeah, without using arrays. 回答1: A perfect recursion problem to tackle if you're new to programming... 4692/1000 = 4 4692%1000 = 692 692/100 = 6 692%100 = 92 92/10 = 9 92%10 = 2 You should get the idea for the loop you should use now, so that it works for any number. :) 回答2: Haven't written C code

Rounding to n significant digits

二次信任 提交于 2019-11-26 18:36:14
问题 I'm trying to write code in MATLAB that will round number to certain (as I ask) significant digits. I'm not sure how to do it. Any suggestions? 回答1: To round to d significant digits : >> d = 3; %// number of digits >> x = 5.237234; %// example number >> D = 10^(d-ceil(log10(x))); >> y = round(x*D)/D y = 5.2400 To round to d decimal digits : >> d = 3; %// number of digits >> x = 5.237234; %// example number >> D = 10^d; >> y = round(x*D)/D y = 5.2370 EDIT Actually it's easier: the round

Are the character digits ['0'..'9'] required to have contiguous numeric values?

廉价感情. 提交于 2019-11-26 18:31:42
问题 Must a C++ implementation set the chars '0'-'9' to have contiguous numeric values, i.e. so that: '0' -> 0+n '1' -> 1+n m -> m+n '9' -> 9+n I cannot find it mentioned in the documentation of isdigit ([classification] (22.3.3.1 Character classification)) * , nor can I find it in the locale documentation (but maybe I did not look hard enough). In 2.3 Character sets, we find that The basic source character set consists of 96 characters: the space character, the control characters representing

Efficient way to determine number of digits in an integer

ⅰ亾dé卋堺 提交于 2019-11-26 18:21:04
What is a very efficient way of determining how many digits there are in an integer in C++? Well, the most efficient way, presuming you know the size of the integer, would be a lookup. Should be faster than the much shorter logarithm based approach. If you don't care about counting the '-', remove the + 1. // generic solution template <class T> int numDigits(T number) { int digits = 0; if (number < 0) digits = 1; // remove this line if '-' counts as a digit while (number) { number /= 10; digits++; } return digits; } // partial specialization optimization for 32-bit numbers template<> int

Weird error in R when importing (64-bit) integer with many digits

这一生的挚爱 提交于 2019-11-26 17:13:51
问题 I am importing a csv that has a single column which contains very long integers (for example: 2121020101132507598) a<-read.csv('temp.csv',as.is=T) When I import these integers as strings they come through correctly, but when imported as integers the last few digits are changed. I have no idea what is going on... 1 "4031320121153001444" 4031320121153001472 2 "4113020071082679601" 4113020071082679808 3 "4073020091116779570" 4073020091116779520 4 "2081720101128577687" 2081720101128577792 5

Check if string contains only digits

六眼飞鱼酱① 提交于 2019-11-26 17:05:29
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 wanna make sure an input contains ONLY digits and no other characters. Since +100 and -5 are both numbers, isNaN() is not the right way to go. Perhaps a regexp is what I need? Any tips? how about var isnum = /^\d+$/.test(val); string.match(/^[0-9]+$/) != null; String.prototype.isNumber = function(){return /^\d+$/.test(this);} console.log("123123".isNumber()); // outputs true console.log("+12".isNumber()); //

Set the digits after decimal point

廉价感情. 提交于 2019-11-26 16:59:00
问题 I have a float number for example 12.12123 Is there a function which would display only number with 2 digits after decimal point 12.12 ? Here is the code: y1 = ( c1 - (a1 * x)) / b1; y2 = ( c2 - a2 * x) / b2; if (y1 == y2) cout << "The same"; so if the y1 = 1.001 and the y2 = 1.002 they do not appear as the same. I tried to add. cout.setf(ios::fixed, ios::floatfield); cout.precision(2); but it does not seem to help. 回答1: /* The C way */ #include <stdio.h> ... float f = 12.12123f; printf("%.2f

Show a leading zero if a number is less than 10 [duplicate]

跟風遠走 提交于 2019-11-26 14:03:14
问题 This question already has answers here : Closed 8 years ago . 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 display 05, rather than 5. How can I do this? I could manually check the number and add a 0 to it as a string, but I was hoping there's a JS function that would do it? 回答1: There's no built