double

Round number to nearest “nth” based on first non zero

瘦欲@ 提交于 2019-12-10 16:54:31
问题 I want to round a Double to the nearest non zero number that follows the decimal. For example: x = 0.002341 rounded = 0.002 x = 0.000048123 rounded = 0.00005 For cases where the base number is > 0, it should perform as such x = 1.000234 rounded = 1.0002 I know I can use Double(round(1000*x)/1000) if I know the number of digits, but I want it to work for any number. Is there a swift function that does this? 回答1: You can have a little fun with logarithms to solve this: func roundFirst(x:Double)

How to know if a double string is round-trip safe?

﹥>﹥吖頭↗ 提交于 2019-12-10 16:44:14
问题 I have a text representation of a double and want to know if it's safe to round-trip it to double and back. How do I know this if I also want to accept any kind of number-style of the input? Or how do I know if any precision is lost when a double-string is parsed with Double.Parse? Or how do I ToString a double to match the same format as another double-string? An answer to any of these questions would be a solution I think. 回答1: Use the R format specifier to convert the double to a string :

IFormatProvider scientific conversion from double to string - number of digits

我是研究僧i 提交于 2019-12-10 15:55:24
问题 I have a problem with the conversion from double to string. I want to convert: double value: 0.0772486324655191 string value: 0.0772486324655191 and if the length is bigger than 16 digits after the decimal point I want it like this: double value: 0.00063500244832493823 string value: 6.3500244832493823e-004 I have tried to convert it with IFormatProvider Pattern: 0.0000000000000000e000 But the result in the first case is 7.7248632465519100e-002 How can I get the number of digits in my double

Division in c# not going the way I expect

£可爱£侵袭症+ 提交于 2019-12-10 15:21:22
问题 Im trying to write something to get my images to show correctly. I have 2 numbers "breedtePlaatje" and "hoogtePlaatje". When i load those 2 vars with the values i get back "800" and "500" i expect "verH" to be (500 / 800) = 0,625. Tho the value of verH = 0.. This is the code: int breedtePlaatje = Convert.ToInt32(imagefield.Width); int hoogtePlaatje = Convert.ToInt32(imagefield.Height); //Uitgaan van breedte plaatje if (breedtePlaatje > hoogtePlaatje) { double verH = (hoogtePlaatje

Use of integers and doubles give different answers when they shouldn't

余生长醉 提交于 2019-12-10 15:04:08
问题 I'm solving a Project Euler Problem 14 using java. I am NOT asking for help solving the problem. I have already solved it, but I ran into something I can't figure out. The problem is like this: The following iterative sequence is defined for the set of positive integers: n = n/2, if n is even n = 3n + 1, if n is odd Using the rule above and starting with 13, we generate the following sequence: 13 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1. Here, the length of the chain is 10 numbers.

Using primitives or wrapper class in Hibernate?

随声附和 提交于 2019-12-10 14:58:13
问题 Mapping database with Hibernate. We should use Double with @NotNull constraint Or use the double primitive type instead. What is the best practice? (Using Java 6) @Column(name = "price_after_tax", nullable=false) @NotNull public Double getPriceAfterTax() { return priceAfterTax; } OR @Column(name = "price_after_tax") public double getPriceAfterTax() { return priceAfterTax; } Many thanks! 回答1: I think you should use Double as it can hold even null value. So, in future if by any chance you

Handling CGFloat with an NSScanner on arm64

孤者浪人 提交于 2019-12-10 14:55:50
问题 Apparently CGFloat is double on arm64: #if defined(__LP64__) && __LP64__ # define CGFLOAT_TYPE double # define CGFLOAT_IS_DOUBLE 1 # define CGFLOAT_MIN DBL_MIN # define CGFLOAT_MAX DBL_MAX #else # define CGFLOAT_TYPE float # define CGFLOAT_IS_DOUBLE 0 # define CGFLOAT_MIN FLT_MIN # define CGFLOAT_MAX FLT_MAX #endif So the code NSScanner *scanner = [NSScanner scannerWithString:string]; CGFloat c[components]; [scanner scanFloat:&c[i]] which was working fine for 32-bit apps, is broken for 64-bit

Round double to number in interval, defining by step

蓝咒 提交于 2019-12-10 14:51:52
问题 I have some abstract double interval, defining by step f.e.: 0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0 - where interval == 0.1 0.0, 0.25, 0.5, 0.75, 1.0 - where interval == 0.25 0.0, 0.5, 1.0 - where interval == 0.5 Does Java have some instrument to "round" some double to closest number, according to interval? f.e: 0.511111 - to 0.5 in first case 0.599999 - to 0.6 in first case 0.511111 - to 0.5 in second case 0.599999 - to 0.5 in second case 0.711111 - to 0.75 in second case 0

How can I add a thousands separator to a double in C on Windows?

余生长醉 提交于 2019-12-10 14:37:43
问题 I use the MPFR library to do calculations on big numbers, but also return a double with 8 digits after the decimal point. I mpfr_sprintf the number to a char array so precision or anything isn't lost. Everything is fine except that I didn't find any thousand separator option in the documentation(or I missed it). Given a number such as 20043.95381376 I would like to represent it like 20,043.95381376 for better readability. Or the number 164992818.48075795 as 164,992,818.48075795 I read about

What is the difference between new Double(someString) and Double.parseDouble(someString)

岁酱吖の 提交于 2019-12-10 13:33:45
问题 As far as I can tell new Double(someString) and Double.parseDouble(someString) give me the exact same result. Is there any reason I would want to use one over the other? 回答1: One returns Double ; the other, double . The differences between primitive Java types and their wrapper counterparts are discussed, for example, here. 来源: https://stackoverflow.com/questions/9004370/what-is-the-difference-between-new-doublesomestring-and-double-parsedoublesom