tostring

Why is a round-trip conversion via a string not safe for a double?

 ̄綄美尐妖づ 提交于 2019-11-27 07:01:48
Recently I have had to serialize a double into text, and then get it back. The value seems to not be equivalent: double d1 = 0.84551240822557006; string s = d1.ToString("R"); double d2 = double.Parse(s); bool s1 = d1 == d2; // -> s1 is False But according to MSDN: Standard Numeric Format Strings , the "R" option is supposed to guarantee round-trip safety. The round-trip ("R") format specifier is used to ensure that a numeric value that is converted to a string will be parsed back into the same numeric value Why did this happen? Mehrdad I found the bug. .NET does the following in clr\src\vm

Is it really worth implementing toString() for entity classes

旧时模样 提交于 2019-11-27 05:59:34
问题 It is consistently advised to override (implement) the toString() method of a class. The Java API documentation itself says "It is recommended that all subclasses override this method.". Bloch, in Effective Java has the item "Always override toString". And only a fool contradicts Bloch, right? I am however coming to doubt this advice: is it really worth implementing toString() for entity classes ? I'll try to lay out my reasoning. An entity object has a unique identity; it is never the same

How to print object content in correct way? [duplicate]

别来无恙 提交于 2019-11-27 05:25:24
This question already has an answer here: How do I print my Java object without getting “SomeType@2f92e0f4”? 10 answers I have an ArrayList that contains some objects from User class. When I print these objects I got: [User@18fd984, User@18fd984] How to print these objects in a correct way? Override the method toString in the class to produce the output you prefer, instead of the default value that Java automatically generates. Example: public class User { private String name; ... @Override public String toString() { return name; } } For complex objects, Apache Commons Lang provides some handy

Reverse (parse the output) of Arrays.toString(int[]) [duplicate]

怎甘沉沦 提交于 2019-11-27 05:15:24
This question already has an answer here: Convert String to int array in java 9 answers Is there in the JDK or Jakarta Commons (or anywhere else) a method that can parse the output of Arrays.toString, at least for integer arrays? int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} ); Sam Pretty easy to just do it yourself: public class Test { public static void main(String args[]){ int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} )); } private static int[] fromString(String string) { String[] strings = string.replace("[", "").replace("]", "").split(", "); int result[] = new int

Double ToString - No Scientific Notation [duplicate]

假如想象 提交于 2019-11-27 05:15:00
This question already has an answer here: Double to string conversion without scientific notation 17 answers I just came across the wonderful "feature" that .NET will by default do Double.ToString() using scientific notation if there are enough decimal places. .005.ToString() // ".005" .00005.ToString() // "5E-05" Does anyone know an efficient way to get this to appear as a string in standard (non-scientific) notation? I've seen this question , but the top two answers seem kind of hacky to me as they both do Double.ToString() and then just reformat the results. Thanks for any help. EDIT: The

js你不知的那些基础问题-对象

别来无恙 提交于 2019-11-27 05:02:03
1 属性的删除:delete 命令 delete 命令用于删除对象的属性,删除成功后返回 true 。 var obj = { p: 1 }; Object.keys(obj) // ["p"] delete obj.p // true obj.p // undefined Object.keys(obj) // []    上面代码中, delete 命令删除对象 obj 的 p 属性。删除后,再读取 p 属性就会返回 undefined ,   而且 Object.keys 方法的返回值也不再包括该属性。    注意,删除一个不存在的属性, delete 不报错,而且返回 true 。 var obj = {}; delete obj.p // true    上面代码中,对象 obj 并没有 p 属性,但是 delete 命令照样返回 true 。   因此,不能根据 delete 命令的结果,认定某个属性是存在的。   只有一种情况, delete 命令会返回 false ,那就是该属性存在,且不得删除 var obj = Object.defineProperty({}, 'p', { value: 123, configurable: false }); obj.p // 123 delete obj.p // false    上面代码之中,对象 obj 的 p

How to create an extension method for ToString?

若如初见. 提交于 2019-11-27 04:26:57
问题 I have tried this: public static class ListHelper { public static string ToString<T>(this IList<String> list) { return string.Join(", ", list.ToArray()); } public static string ToString<T>(this String[] array) { return string.Join(", ", array); } } But it does not work, both for string[] and List<string> . Maybe I need some special annotations? 回答1: Extension methods are only checked if there are no applicable candidate methods that match. In the case of a call to ToString() there will always

How can I override the toString method of an ArrayList in Java?

笑着哭i 提交于 2019-11-27 03:23:58
问题 I would like to have my own implementation of the toString() method for an ArrayList in Java. However, I can't get it working even though I added my toString() like this to the class that contains the ArrayList. @Override public String toString() { String result = "+"; for (int i = 0; i < list.size(); i++) { result += " " + list.get(i); } return result; } When I call my ArrayList like this list.toString() , I still get the default representation. Am I missing something? 回答1: You should do

TO_CHAR of an Oracle PL/SQL TABLE type

情到浓时终转凉″ 提交于 2019-11-27 03:05:48
问题 For debugging purposes, I'd like to be able to " TO_CHAR " an Oracle PL/SQL in-memory table. Here's a simplified example, of what I'd like to do: DECLARE TYPE T IS TABLE OF MY_TABLE%ROWTYPE INDEX BY PLS_INTEGER; V T; BEGIN -- .. -- Here, I'd like to dbms_output V's contents, which of course doesn't compile FOR i IN V.FIRST .. V.LAST LOOP dbms_output.put_line(V(i)); END LOOP; -- I want to omit doing this: FOR i IN V.FIRST .. V.LAST LOOP dbms_output.put_line(V(i).ID || ',' || V(i).AMOUNT ...);

When is it desired to not implement toString() in Java?

*爱你&永不变心* 提交于 2019-11-27 03:01:46
问题 A lead developer on my project has taken to referring to the project's toString() implementations as "pure cruft" and is looking to remove them from the code base. I've said that doing so would mean that any clients wishing to display the objects would have to write their own code to convert the object to string, but that was answered with "yes they would". Now specifically, the objects in this system are graphic elements like rectangles, circles, etc and the current representation is to