tostring

What are the original reasons for ToString() in Java and .NET?

三世轮回 提交于 2019-12-17 16:22:07
问题 I've used ToString() modestly in the past and I've found it very useful in many circumstances. However, my usage of this method would hardly justify to put this method in none other than System.Object . My wild guess is that, at some point during the work carried out and meetings held to come up with the initial design of the .NET framework, it was decided that it was necessary - or at least extremely useful - to include a ToString() method that would be implemented by everything in the .NET

Negative numbers to binary string in JavaScript

早过忘川 提交于 2019-12-17 15:23:15
问题 Anyone knows why javascript Number.toString function does not represents negative numbers correctly? //If you try (-3).toString(2); //shows "-11" // but if you fake a bit shift operation it works as expected (-3 >>> 0).toString(2); // print "11111111111111111111111111111101" I am really curious why it doesn't work properly or what is the reason it works this way? I've searched it but didn't find anything that helps. 回答1: Short answer: The toString() function takes the decimal, converts it to

Is toString() only useful for debugging?

假装没事ソ 提交于 2019-12-17 13:53:23
问题 Besides of course, their use with primitives. Most (if not all) of the implementations I see are only useful from a programmer viewpoint. EDIT : I understand that I'm supposed to override the default behavior, that's why I mentioned implementations :). And I do get the value of overriding it in some components requiring a String representation inside a GUI. However, in the JDK at least, I see lots of implementations that only come to use whenever you need to debug the object instances. Why is

C# Converting 20 digit precision double to string and back again

只愿长相守 提交于 2019-12-17 10:58:15
问题 In C#. I have a double (which I've extracted from a database) that has 20 digit precision. In Visual Studio (using QuickWatch) I can see the value of the double to be = 0.00034101243963859839. I want to display this value in a textbox and then have it be the same value when I take it out and convert it back into a double. But I always lose the last two digits I've tried the following: double d = 0.00034101243963859839; string s = d.ToString(); string s2 = d.ToString("F20"); string s3 = d

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

大兔子大兔子 提交于 2019-12-17 08:53:43
问题 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

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

我怕爱的太早我们不能终老 提交于 2019-12-17 07:47:56
问题 This question already has answers here : How do I print my Java object without getting “SomeType@2f92e0f4”? (10 answers) Closed 4 years ago . 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? 回答1: 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

Convert xml to string with jQuery

放肆的年华 提交于 2019-12-17 03:09:16
问题 I'm loading an xml file with jQuery ajax loader, and need to convert it to a string so that I can save it out again using PHP post variables. What is the best way to do this? <script type='text/javascript'> jQuery.ajax({ type: "GET", url: "data.xml", dataType: "xml", success: parseXML }); function parseXML(xml) { var xml_string = jQuery(xml).text(); // (This doesn't work- returns tagless, unformatted text) alert(xml_string); } </script> 回答1: Here it is: <script type='text/javascript'>

How to convert an int array to String with toString method in Java [duplicate]

最后都变了- 提交于 2019-12-16 22:47:07
问题 This question already has answers here : What's the simplest way to print a Java array? (33 answers) Closed 3 years ago . I am using trying to use the toString(int[]) method, but I think I am doing it wrong: http://docs.oracle.com/javase/1.5.0/docs/api/java/util/Arrays.html#toString(int[]) My code: int[] array = new int[lnr.getLineNumber() + 1]; int i = 0; System.out.println(array.toString()); The output is: [I@23fc4bec Also I tried printing like this, but: System.out.println(new String()

js类型检测

此生再无相见时 提交于 2019-12-16 22:28:56
大约是三月初吧,在网上看到一道面试题,怎么判断一个变量类型是不是数组。然后从犀牛书以及查阅一些资料得到了答案。 这里分为四种情况分析: 通过constructor [].constructor === Array; //true 这种方法比较坑,不推荐,因为constructor是可以自己修改的。 通过instanceof [] instanceof Array; //true 犀牛书给出的解释是在页面中含多个窗体或者iframe,那么会产生很多执行环境,一个iframe下的数组不是另一个窗体下构造函数的实例。那么如果出现这种极端条件,instanceof也不推荐使用。 通过Array.isArray 在新版浏览器,IE9+都已经实现了原生方法 Array.isArray([1,2]); //true 通过toSting Object.prototype.toString.call([]) === '[object Array]' 在一些博客和犀牛书给出的都是这种方法。 因为之前对call语法,toString方法的疑惑,导致我只能死记硬背这句代码。最近在回顾继承有关知识,学到了call方法,这段代码意思是对[]调用Object对象原型下的toString方法,不同于数组原型下的toString的方法,之所以疑惑,因为数组下的toString是把一个数组转成字符串

What is this: [Ljava.lang.Object;?

☆樱花仙子☆ 提交于 2019-12-16 22:08:14
问题 I get this when I call toString on an object I received from a function call. I know the type of the object is encoded in this string, but I don't know how to read it. What is this type of encoding called? 回答1: [Ljava.lang.Object; is the name for Object[].class , the java.lang.Class representing the class of array of Object . The naming scheme is documented in Class.getName(): If this class object represents a reference type that is not an array type then the binary name of the class is