digits

Rounding to n significant digits

白昼怎懂夜的黑 提交于 2019-11-27 16:12:15
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? 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 function supports these options: >> d = 3; >> x = 5.237234; >> y = round(x, d, 'significant') y = 5.2400 >> d = 3;

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

☆樱花仙子☆ 提交于 2019-11-27 15:36:25
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 "4041720081087539887" 4041720081087539712 6 "4011120071074301496" 4011120071074301440 7 "4021520051054304372

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

假如想象 提交于 2019-11-27 15:27:46
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 horizontal tab, vertical tab, form feed, and new-line, plus the following 91 graphical characters But it

Set the digits after decimal point

佐手、 提交于 2019-11-27 15:02: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. Matteo Italia /* The C way */ #include <stdio.h> ... float f = 12.12123f; printf("%.2f",f); // The C++ way #include <iostream> ... float f = 12.12123f; std::cout.setf(std::ios_base

Finding the number of digits of an integer

╄→гoц情女王★ 提交于 2019-11-27 09:46:02
问题 This question was migrated from Software Engineering Stack Exchange because it can be answered on Stack Overflow. Migrated 8 years ago . 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

Get number of digits with JavaScript

风流意气都作罢 提交于 2019-11-27 09:15:44
问题 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 ? 回答1: length is a property, not a method. You can't

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

≡放荡痞女 提交于 2019-11-27 08:32:45
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? Chris Fulstow There's no built-in JavaScript function to do this, but you can write your own fairly easily: function pad(n) { return (n < 10) ? ("0" + n) : n; } EDIT: Meanwhile there is a

What programming language will enable me to enter a very long number without converting it to floating point?

你离开我真会死。 提交于 2019-11-27 07:58:17
问题 What would be the best way to do the following. Enter a very long number, lets say 500,000 digits long without it going into scientific notation; and then am able to do math with it, like +2 etc.? Thank you in advance. EDIT: It is a 500,000 digit, positive integer. 回答1: Python and Java have native support, libraries exist for C++, C, .NET, ... 回答2: I know that Erlang has support for unlimited size int arithmetics. 回答3: Python does this out of the box with no special library. So does 'bc'

How to find out if letter is Alphanumeric or Digit in Swift

混江龙づ霸主 提交于 2019-11-27 07:47:29
I want to count the number of letters, digits and special characters in the following string: let phrase = "The final score was 32-31!" I tried: for tempChar in phrase { if (tempChar >= "a" && tempChar <= "z") { letterCounter++ } // etc. but I'm getting errors. I tried all sorts of other variations on this - still getting error - such as: could not find an overload for '<=' that accepts the supplied arguments Update for Swift 3: let letters = CharacterSet.letters let digits = CharacterSet.decimalDigits var letterCount = 0 var digitCount = 0 for uni in phrase.unicodeScalars { if letters

Gets last digit of a number

我怕爱的太早我们不能终老 提交于 2019-11-27 04:45:15
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 = new int[temp.length()]; int last = guess[temp.length() - 1]; return last; } Question: How to solve this issue? Just return (number % 10) ; i.e. take the modulus. This will be much faster than parsing in and out of a string. If number can be negative then use (Math.abs(number) % 10); Below is a simpler solution how to get the last digit from an int : public