decimal

set double format with 2 decimal places [duplicate]

拟墨画扇 提交于 2019-12-18 07:04:27
问题 This question already has answers here : Round a double to 2 decimal places [duplicate] (13 answers) Closed 6 years ago . I have a short equation: double compute,computed2; compute=getminutes/60; where getminutes is int and I want to set the equivalent of compute with 2 decimal places. 0.00 how can I format the equivalent with 2 decimal places? Example: compute=45/60 it should be 0.75 Here is my work: DecimalFormat df2 = new DecimalFormat("00.00000"); double computed,computed2 = 00.000;

set double format with 2 decimal places [duplicate]

霸气de小男生 提交于 2019-12-18 07:04:06
问题 This question already has answers here : Round a double to 2 decimal places [duplicate] (13 answers) Closed 6 years ago . I have a short equation: double compute,computed2; compute=getminutes/60; where getminutes is int and I want to set the equivalent of compute with 2 decimal places. 0.00 how can I format the equivalent with 2 decimal places? Example: compute=45/60 it should be 0.75 Here is my work: DecimalFormat df2 = new DecimalFormat("00.00000"); double computed,computed2 = 00.000;

Converting a decimal value to a 32bit floating-point hexadecimal

陌路散爱 提交于 2019-12-18 06:59:54
问题 For a simple utility I'm working on, I need a script that converts a given decimal value to a 32bit floating-point hexadecimal value. For example, I know 1 is 3F800000 and 100 is 42C80000, however I don't know how to return these results with any number. If somebody knows a simple formula or even a complex way to go about doing this, please share. 回答1: I don't know if I got the corner cases correctly, but anyway, here is some code: function floatToIntBits(f) { var NAN_BITS = 0|0x7FC00000; var

Decimal to binary (and vice-versa)

白昼怎懂夜的黑 提交于 2019-12-18 06:48:12
问题 Can anybody give an example of c++ code that can easily convert a decimal value to binary and a binary value to decimal please? 回答1: Well, your question is really vague, so this answer is the same. string DecToBin(int number) { if ( number == 0 ) return "0"; if ( number == 1 ) return "1"; if ( number % 2 == 0 ) return DecToBin(number / 2) + "0"; else return DecToBin(number / 2) + "1"; } int BinToDec(string number) { int result = 0, pow = 1; for ( int i = number.length() - 1; i >= 0; --i, pow

JavaScript: Rounding Down in .5 Cases

让人想犯罪 __ 提交于 2019-12-18 06:13:20
问题 I am in a situation where a JavaScript function produces numbers, such as 2.5 . I want to have these point five numbers rounded down to 2 , rather than the result of Math.round , which will always round up in such cases (ignoring the even odd rule), producing 2. Is there any more elegant way of doing this than subtracting 0.01 from the number before rounding? Thanks. 回答1: Just negate the input and the output to Math.round : var result = -Math.round(-num); In more detail: JavaScript's Math

Standard for the sine of very large numbers

醉酒当歌 提交于 2019-12-18 05:56:19
问题 I am writing an (almost) IEEE 854 compliant floating point implementation in TeX (which only has support for 32-bit integers). This standard only specifies the result of + , - , * , / , comparison, remainder, and sqrt : for those operations, the result should be identical to rounding the exact result to a representable number (according to the rounding mode). I seem to recall that IEEE specifies that transcendental functions ( sin , exp ...) should yield faithful results (in the default round

How can I limit the number of digits displayed by printf after the decimal point?

社会主义新天地 提交于 2019-12-18 05:54:39
问题 I wrote a small program that reads two integers using scanf and then performs various arithmetic calculations. I'm using printf to display the results. How can I make printf display only two digits after the decimal point? Starting with the simplified code sample: #include <stdio.h> int main(void) { double third = 1.0 / 3.0; // display data printf("\n%20s%20s", "Description", "Data"); printf("\n%20s%20s", "-----------", "----"); printf("\n%20s%20lf", "One third", third); printf("\n"); return

How to prevent user from entering decimals?

安稳与你 提交于 2019-12-18 05:48:09
问题 I've got an order page on my site. Some order items can have decimal quantities, some can't. What's the best way to prevent the user from entering decimal quantities? (Apart from an alert box and setting the field to zero)? 回答1: Intercept key events for the field, detect illegal characters upon input, keep them from getting entered in the field and add a temporary msg near the field (inserted into the page) that explains what characters or values are allowed. pop up alerts are a bad way to

Decimal - truncate trailing zeros

萝らか妹 提交于 2019-12-18 05:43:52
问题 I noticed that .NET has some funky/unintuitive behavior when it comes to decimals and trailing zeros. 0m == 0.000m //true 0.1m == 0.1000m //true but (0m).ToString() == (0.000m).ToString() //false (0.1m).ToString() == (0.1000m).ToString() //false I know about necessity to comply to the ECMA CLI standard. However I would like to know if there is built-in way to truncate the trailing zeros for a decimal value without going through string representation (.ToString("G29") and parse back trick

C# how to always round down to nearest 50

大城市里の小女人 提交于 2019-12-18 04:34:23
问题 I've done a search on C# rounding, but haven't been able to find the answer to my current problem. What I want to do is always round down to the nearest 50. All the values I want to round down will be in decimal. So 635.25 would be 600. 298.42 would be 250. 149.56 would be 100. I've looked at math.round but how would I use that so it always rounds down to the nearest 50 and never up? 回答1: Divide the value by 50, round down to the closest whole number, and multiply by 50 again: double n = Math