double

multiple arguments in a printf method

为君一笑 提交于 2019-12-25 04:04:04
问题 I am writing a code for my Java class that calls for me to create something that looks like a bill/receipt. similar to: Item quantity price total gum 10 $1.49 $14.90 soda 3 $1.00 $3.00 chips 20 $1.25 $20.00 I am using the printf(); method. The variables i have declared are double gumPrice = 1.49; float gumQuantity = 10; double gumTotal = 14.90; double sodaPrice = 1.00; float sodaQuantity = 3; double sodaTotal = 3.00; double chipsPrice = 1.25; float chipsQuantity = 20; double chipsTotal = 25

Java - Format numbers in scientific notation

岁酱吖の 提交于 2019-12-25 03:53:21
问题 I want to format this String : String number = "3.4213946120686956E-9"; to this: String number = "3.42E-9"; Rounding to 2 decimals. How can I achieve it? 回答1: Try new DecimalFormat("0.##E0") . Javadoc: DecimalFormat Example: public class Test { private static java.text.DecimalFormat sf = new java.text.DecimalFormat("0.##E0"); public static void main(String[] args) { System.out.println(sf.format(Double.parseDouble("3.4213946120686956E-9"))); } }; Output: 3.42E-9 回答2: This is not the most

ASP.NET MVC 5 double value always 0.0 in controller

ぐ巨炮叔叔 提交于 2019-12-25 03:26:43
问题 In my ASP.NET MVC 5 app I have form where user can enter price. Of course that value isn't always integer so I defined it as double in my Model. And when User enter some value, e.g. 10000.00 and press submit, model sent to controller always have 0.0 value for property price. Any ideas why ? My property is defined in model as: [Required(ErrorMessageResourceType = typeof(Resources), ErrorMessageResourceName = "_errorEmpty")] [Display(Name = "_carPrice", ResourceType = typeof(Resources))] public

How to weigh up calculation error

試著忘記壹切 提交于 2019-12-25 03:12:31
问题 Consider the following example. There is an image where user can select rectangular area (part of it). The image is displayed with some scale. Then we change the scale and we need to recalculate the new coordinates of selection. Let's take width, newSelectionWidth = round(oldSelectionWidth / oldScale * newScale) where oldScale = oldDisplayImageWidth / realImageWidth , newScale = newDisplayImageWidth / realImageWidth , all the values except for scales are integers. The question is how to prove

Can't affect values to a simple Double[] table which is always null

女生的网名这么多〃 提交于 2019-12-25 03:10:41
问题 I have a text file containing NMEA frames. I retrieve the latitude and the longitude of $GPGGA and $GPRMC frames. For this part, it was okay. Now, I'd like to convert the latitude and longitude into Decimal Degrees. The problem occur when I try to affect a values to Double[]coordinatestoconvert . This one is always null. It's like this error is really idiot, but I turn around all this morning for such a foolishness... Can someone help me please ? Here are the methods I am using : public

Make sure c++ decimal comparison is correct

a 夏天 提交于 2019-12-25 02:57:33
问题 I have two double variable. double a = 0.10000, double b = 0.1. How can I make sure the comparison (a == b) is always true ? 回答1: If you are being paranoid about using == on doubles or floats (which you should be) you can always check that they are close within a small tolerance. bool same = fabs(a-b) < 0.000001; 来源: https://stackoverflow.com/questions/29522171/make-sure-c-decimal-comparison-is-correct

underflow and log values in java

北慕城南 提交于 2019-12-25 02:28:19
问题 I have a question in regard to dealing with small probabilities values in machine learning models. The standard way to avoid underflow problems which results from multiplying small floating-point numbers is to use log(x) instead of x suppose that x=0.50 the log of which is log(x)=-0.301029996 to recover x later on the value of exp(log(x)) != x that is 0.740055574 != 0.50 So, how is using the logarithm is useful to deal with underflow?? 回答1: This has nothing to do with the overflow. In the

Condition to check double is an integer not working

人走茶凉 提交于 2019-12-25 01:25:21
问题 This program was supposed to output zero digits after its decimal point when i is an integer or has no non-zero digit after decimal point. It works for all the cases just not for the last. Can anyone help me fix this? Code : #include <stdio.h> #include<math.h> int main() { double i,j,k; for(i=0;i<=2;i=i+0.2) { k=3; j=i+1; while(k--) { if(fmod(i,1)==0) printf("I=%.0lf J=%.0lf\n",i,j); else printf("I=%.1lf J=%.1lf\n",i,j); j++; } } } Here's the ideone sample execution 回答1: Depending on your

Java int vs. Double

限于喜欢 提交于 2019-12-25 00:43:20
问题 public double calc(int v1) { return v1 / 2 + 1.5; } public double cald (double v) { return v / 2 + 1.5; } Do the functions return the same result? I would argue that they don't return the same result, as the second function would include the decimal point, where as the second function would round the number up. Is that correct? 回答1: when you divide a by b i.e a/b if both a & b are int then result will be int else any or both a & b are double then result will be double Edit: Also see my answer

Is this an overflow?

孤街醉人 提交于 2019-12-25 00:33:36
问题 I have this segment of code: struct timeval start, end; gettimeofday(&start, NULL); //code I'm timing gettimeofday(&end, NULL); long elapsed = ((end.tv_sec-start.tv_sec)*1000000 + end.tv_usec-start.tv_usec); ofstream timeFile; timeFile.open ("timingSheet.txt"); timeFile << fixed << showpoint; timeFile << setprecision(2); timeFile << "Duration: " << elapsed << "\n"; timeFile.close(); Which will output the number of microseconds that has passed. However, if I change this line long elapsed = (