decimal

Conversion from double to integer [duplicate]

时光毁灭记忆、已成空白 提交于 2019-12-04 06:24:02
问题 This question already has answers here : C++: How to round a double to an int? [duplicate] (5 answers) round() for float in C++ (21 answers) Closed 5 years ago . I am stuck in problem where the double number is not getting properly converted to integer. In this case-> int x=1000; double cuberoot=pow(x,(1/(double)3)); int a=cuberoot; cout<<"cuberoot="<<cuberoot<<endl; cout<<"a="<<a<<endl; Output: cuberoot=10 a=9 Why here a=9 and not 10? Any solution to this problem?? Also I don't want to round

Calculate large decimals (more than 15 digits) in Javascript

陌路散爱 提交于 2019-12-04 06:10:07
问题 As we know, Javascript has some issues (or features) with calculating decimal numbers. For example: console.log(0.1 + 0.2) // 0.30000000000000004 And we know that we can avoid it using different libraries (for example I use bignumber.js), and now we have what expected: console.log(Number(new BigNumber(0.1).plus(0.2))); // 0.3 <script src="https://cdnjs.cloudflare.com/ajax/libs/bignumber.js/6.0.0/bignumber.min.js"></script> BUT Bignumber.js has limitations: It accepts a value of type number

Why HTML decimal and HTML hex?

倖福魔咒の 提交于 2019-12-04 05:39:23
I have tried to Google quite a while now for an answer why HTML entities can be compiled either in HTML decimal or HTML hex. So my questions are: What is the difference between HTML decimal and HTML hex? Why are there two systems to do the same thing? Originally, HTML was nominally based on SGML, which has decimal character references only. Later, the hexadecimal alternative was added in HTML 4.01 (and soon implemented in browsers), then retrofitted into SGML in the Web Adaptations Annex . The apparent main reason for adding the hexadecimal alternative was that all modern character code and

Python cdecimal InvalidOperation

感情迁移 提交于 2019-12-04 03:52:00
I am trying to read financial data and store it. The place I get the financial data from stores the data with incredible precision, however I am only interested in 5 figures after the decimal point. Therefore, I have decided to use t = .quantize(cdecimal.Decimal('.00001'), rounding=cdecimal.ROUND_UP) on the Decimal I create, but I keep getting an InvalidOperation exception. Why is this? >>> import cdecimal >>> c = cdecimal.getcontext() >>> c.prec = 5 >>> s = '45.2091000080109' >>> # s = '0.257585003972054' works! >>> t = cdecimal.Decimal(s).quantize(cdecimal.Decimal('.00001'), rounding

How to Convert pythons Decimal() type into an INT and exponent

北慕城南 提交于 2019-12-04 03:13:46
I would like to use the Decimal() data type in python and convert it to an integer and exponent so I can send that data to a microcontroller/plc with full precision and decimal control. https://docs.python.org/2/library/decimal.html I have got it to work, but it is hackish; does anyone know a better way? If not what path would I take to write a lower level "as_int()" function myself? Example code: from decimal import * d=Decimal('3.14159') t=d.as_tuple() if t[0] == 0: sign=1 else: sign=-1 digits= t[1] theExponent=t[2] theInteger=sign * int(''.join(map(str,digits))) theExponent theInteger For

Counting the number of decimal places in pascal

心已入冬 提交于 2019-12-04 02:29:55
问题 I just started studying pascal and I have to do a pascal program as homework. I made it but I don't know how to count the number of decimal places in a real number (the number of digit after the "."). I need it just to format well a real number (like write(real:0:dec) where dec is the number of decimal digit i don't know how to know). I'd like to do that because i don't want it in scientific notation or with many unnecessary zeros. For example if a real number is 1.51 (x) and I write writeln

How to avoid rounding off in NSNumberFormatter

丶灬走出姿态 提交于 2019-12-04 01:57:48
I am trying to have a number string with maximum 2 decimals precision, while rest decimals just trimmed off instead of rounding them up. For example: I have: 123456.9964 I want: 123456.99 -> Just want to trim rest of the decimal places What I have tried is: NSNumberFormatter *numberFormatter = [[NSNumberFormatter alloc] init]; [numberFormatter setNumberStyle: NSNumberFormatterDecimalStyle]; [numberFormatter setMaximumFractionDigits:2]; NSString *numberAsString = [numberFormatter stringFromNumber:[NSNumber numberWithFloat: 123456.9964]]; NSLog(@"%@", numberAsString); There is nothing to set

Is there a way to round a decimal place to the nearest whole in javascript?

只谈情不闲聊 提交于 2019-12-04 01:34:55
问题 I have a number with decimal places and I am wondering if it's possible to round the decimal to the nearest whole using javascript? My number is: 4.59 I need my number to round to: 4.60 回答1: Use Number.toFixed(number of decimal places): var num = 4.59; var rounded = num.toFixed(1); 回答2: Use the toFixed() method. More detailed information at: MDN :: toFixed 回答3: var x = 4.5678; Math.round(x * 10) / 10; // 4.6 Math.round(x * 100) / 100; // 4.57 Where the number of 0 s of multiplication and

C#: Storing percentages, 50 or 0.50?

六月ゝ 毕业季﹏ 提交于 2019-12-04 00:13:16
When holding percentage values in variables is there a preference between holding them as whole numbers vs fractions. That is should the variable hold numbers between 0 and 100 or between 0.00 and 1.00? In either case, the variable holding the values is of decimal type. The database I'm interacting with happens to store them as whole numbers 0 to 100. Note: I'm using the word "whole number" to denote values in the range 0 to 100 though those values may contain fractional components (e.g., 25.75). I don't know how else to describe the difference between the two ranges of percentage values I

Calculating nth root in Java using power method

此生再无相见时 提交于 2019-12-03 23:01:49
I was trying to get a cubic root in java using Math.pow(n, 1.0/3) but because it divides doubles, it doesn't return the exact answer. For example, with 125, this gives 4.9999999999. Is there a work-around for this? I know there is a cubic root function but I'd like to fix this so I can calculate higher roots. I would not like to round because I want to know whether a number has an integer root by doing something like this: Math.pow(n, 1.0 / 3) % ((int) Math.pow(n, 1.0 / 3)) . Tunaki Since it is not possible to have arbitrary-precision calculus with double , you have three choices: Define a