During the previous interview, an interview question is often given to the interviewer. Given a string, the value of the floating point indicated by this string is output. The requirements are as follows:
Write a conversion function. The input of this function is a string that represents a floating point number. Convert
The Int, Char, long, float, and double types of Java are converted to bytes.
16:53:40
Add to favorites I want to contribute
[Font: small and big]
Java codePackage com. util;/**** * * * * * * * * * ** @ Version 1.0* @ Author Xu liduo*/Public class byteutil {/*** Convert short to byte.** @ Param
above. This is not to mention. The second problem is that some tasks are wrong tasks. Once they are run, they disappear immediately, therefore, the consumption data of such tasks is very small. For example, when the CPU is running for 0.000001 seconds, after the data becomes float, EC performs a short operation (because the number of decimal places is too long, EC only keeps the four digits after the decimal point, and does
, whereas three virtual arrows represent conversions that may lose precision.
3. floating-point type float, double data is not suitable for the area of financial computation that does not allow rounding errors.
For example, the above test2, we expect the result is 0.1, but the actual output is 0.8999999999999999.
This error is caused by the fact that the floating-point number is actually represented by a binary system. The point 1/10 is not exa
, scale, bigdecimal.round_half_up). Doublevalue (); } /*** Provides a precise small number of four round-off five into the processor *@paramargs*/ Public Static DoubleRoundDoubleVintScale ) { if(scale) { Throw NewIllegalArgumentException ("The scale must is a positive integer or zero"); } BigDecimal b=NewBigDecimal (double.tostring (v)); BigDecimal One=NewBigDecimal ("1"); returnb.divide (one, scale, Bigdecimal.round_half_down). Doublevalue (); } Public Static v
Java codePackage com. util;/**** * * * * * * * * * ** @ Version 1.0* @ Author Xu liduo*/Public class ByteUtil {/*** Convert short to byte.** @ Param B* @ Param s* Short to be converted* @ Param index*/Public static void putShort (byte B [], short s, int index ){B [index + 1] = (byte) (s> 8 );B [index + 0] = (byte) (s> 0 );}/*** Get short from byte array** @ Param B* @ Param index* Number of digits to start*
Java currently follows the ieee-developed floating-point notation for float,double operations. This structure is a scientific notation, denoted by the symbol, exponent, and mantissa, and the base is set to 2--, which means that a floating-point number is represented as the mantissa multiplied by 2 exponent and then added to the Symbol. Let's look at a Java code:p
Example 1Float f = 34.237323f;BigDecimal B = new BigDecimal (f );Float f1 = B. setScale (2, BigDecimal. ROUND_HALF_UP). floatValue ();System. out. println (f1 );Method 2:Float f = 34.232323;BigDecimal B = new BigDecimal (f );Float f1 = B. setScale (2, BigDecimal. ROUND_HALF_UP). floatValue ();// B. setScale (2, BigDecimal. ROUND_HALF_UP) indicates rounding and re
Convert Java basic data to Json code, and convert java data to json
JSON is a lightweight data exchange format, which is very suitable for the interaction between servers and JavaScript. It is the best choice for Data Interaction between data clients and servers in mobile development. Especially in HTML5, it is more i
Run the code in JavaSystem.out.println (2.00-1.10);The output results are: 0.8999999999999999It's weird, not the value we're looking for 0.9.Run the following code again:System.out.println (2.00f-1.10f);Output Result: 0.9And rightly so, why does this cause this problem. Why do you try to avoid floating-point comparisons in your program?In Java, the floating-point type is double, and 2.00 and 1.10 are converted to binary storage in the computer, which
Java float retains two decimal places or decimal placesMethod 1: Use Math.Round to calculate the number format that is returned here.float price=89.89;int itemnum=3;float Totalprice=price*itemnum;float num= (float) (Math.Round (totalprice*100)/100); If the exact 4-bit is req
String series in practice c ++ -- specify valid floating point numbers and convert them to string
For example, to display the file size, we can obtain the total bytes of the file from the server. In this way, we need to display bytes, kb, mb, and other units according to the actual situation.
The common practice is to conver
Cause: If you want to convert a float [] to memory data, check that the following two methods can convert float to byte []. Method 1: Java code: import java. nio. byteBuffer; import java
We know that floating-point numbers cannot be accurately represented in a computer, for example, 0.1 is only represented as an approximation in a computer, so the results are unpredictable when dealing with the number of points.In the case of numeric operations, if there are double or float-type floating-point numbers participating in the calculation, there are occasional instances of inaccurate computations. As the following sample code: Package ex;
The double type and float type are primarily designed for use in scientific and engineering calculations and are used for binary floating-point calculations. For the general calculation is usually inaccurate results, so for the ordinary floating-point addition and subtraction, the solution needs to use Int,long,bigdecimal to calculate. 1. Calculate the problem?The calculation results are as follows:Obviously the result is not what we need. The result
the following format:
[{"rowID":"1","Name":"tqtqwet","Comment":"qewrtqwe"},{"rowID":"2","Name":"waf","Comment":"agsadga"},{"rowID":"3","Name":"android","Comment":"fafdadsf"},{"rowID":"4","Name":"android222","Comment":"fasdfas"},{"rowID":"5","Name":"fadfa","Comment":"adgasd"}]
Now I want to convert it to List
> Format, and display it with listView. The first key of hashmap corresponds to "Name", the second key corresponds to "Comment", and the ro
Method 1: Use Math.Round to calculate the number format that is returned here.float price=89.89;int itemNum=3;float totalPrice=price*itemNum;float num=(float)(Math.round(totalPrice*100)/100);//如果要求精确4位就*10000然后/10000Method 2: Return with DecimalFormat in string format. The class is fully encapsulated in decimal. Like t
1, arrarlist conversion to string[]:ArrayList list = new ArrayList ();List. ADD ("AAA");List. ADD ("BBB");String[] arrstring = (string[]) list. ToArray (typeof (String));converting ArrayList to a string array, we can use the[Java]
As you may have encountered, float is not accurate when calculating certain values.For example, the following conditions:Calculation is inaccurate Package com.nicchagil.study.java.demo.No10float calculation. No01 inaccurate examples; Public class Call { publicstaticvoid main (string[] args) { System.out.println ( 0.08f + 0.01f);} }Print:0.089999996Use BigDecimal instead of calculationIf you n
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.