tostring

BigInteger to Hex/Decimal/Octal/Binary strings?

我怕爱的太早我们不能终老 提交于 2019-11-26 07:39:23
问题 In Java, I could do BigInteger b = new BigInteger(500); Then format it as I pleased b.toString(2); //binary b.toString(8); //octal b.toString(10); //decimal b.toString(16); //hexadecimal In C#, I can do int num = int.Parse(b.ToString()); Convert.ToString(num,2) //binary Convert.ToString(num,8) //octal etc. But I can only do it with long values and smaller. Is there some method to print a BigInteger with a specified base? I posted this, BigInteger Parse Octal String?, yesterday and received

How to 'cout' the correct number of decimal places of a double value?

笑着哭i 提交于 2019-11-26 06:46:54
问题 I need help on keeping the precision of a double . If I assign a literal to a double, the actual value was truncated. int main() { double x = 7.40200133400; std::cout << x << \"\\n\"; } For the above code snippet, the output was 7.402 Is there a way to prevent this type of truncation? Or is there a way to calculate exactly how many floating points for a double ? For example, number_of_decimal(x) would give 11, since the input is unknown at run-time so I can\'t use setprecision() . I think I

when to use toString() method

耗尽温柔 提交于 2019-11-26 06:44:03
问题 This may sound very basic... can someone please explain the use of the toString() method and when to effectively use this? Have done a search on google but could not find any good resource. 回答1: In most languages, toString or the equivalent method just guarantees that an object can be represented textually. This is especially useful for logging, debugging, or any other circumstance where you need to be able to render any and every object you encounter as a string. Objects often implement

The connection between &#39;System.out.println()&#39; and &#39;toString()&#39; in Java

半世苍凉 提交于 2019-11-26 06:40:03
问题 What is the connection between System.out.println() and toString() in Java? e.g: public class A { String x = \"abc\"; public String toString() { return x; } } public class ADemo { public static void main(String[] args) { A obj = new A(); System.out.println(obj); } } If main class runs, it gives an output as \"abc\" . When I remove the code which overrides toString() , it gives an output as \"A@659e0bfd\" . So, can anyone explain what is the working principle of System.out.println() when I

Confused about __str__ on list in Python [duplicate]

落花浮王杯 提交于 2019-11-26 06:15:45
问题 This question already has answers here : Python __str__ and lists (9 answers) Closed last year . Coming from a Java background, I understand that __str__ is something like a Python version of toString (while I do realize that Python is the older language). So, I have defined a little class along with an __str__ method as follows: class Node: def __init__(self, id): self.id = id self.neighbours = [] self.distance = 0 def __str__(self): return str(self.id) I then create a few instances of it:

Is it possible to override JavaScript&#39;s toString() function to provide meaningful output for debugging?

♀尐吖头ヾ 提交于 2019-11-26 04:21:42
问题 When I console.log() an object in my JavaScript program, I just see the output [object Object] , which is not very helpful in figuring out what object (or even what type of object) it is. In C# I\'m used to overriding ToString() to be able to customize the debugger representation of an object. Is there anything similar I can do in JavaScript? 回答1: You can override toString in Javascript as well. See example: function Foo() { } // toString override added to prototype of Foo class Foo.prototype

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

ぐ巨炮叔叔 提交于 2019-11-26 03:56:44
This question already has an answer here: How do I print my Java object without getting “SomeType@2f92e0f4”? 10 answers 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().toString(array)); // **error on next line** The method toString() in the type String is not applicable for

Difference between Convert.ToString() and .ToString()

非 Y 不嫁゛ 提交于 2019-11-26 03:49:10
问题 What is the difference between Convert.ToString() and .ToString() ? I found many differences online, but what\'s the major difference? 回答1: Convert.ToString() handles null , while ToString() doesn't. 回答2: Calling ToString() on an object presumes that the object is not null (since an object needs to exist to call an instance method on it). Convert.ToString(obj) doesn't need to presume the object is not null (as it is a static method on the Convert class), but instead will return String.Empty

Convert a vector<int> to a string

天涯浪子 提交于 2019-11-26 03:35:49
问题 I have a vector<int> container that has integers (e.g. {1,2,3,4}) and I would like to convert to a string of the form \"1,2,3,4\" What is the cleanest way to do that in C++? In Python this is how I would do it: >>> array = [1,2,3,4] >>> \",\".join(map(str,array)) \'1,2,3,4\' 回答1: Definitely not as elegant as Python, but nothing quite is as elegant as Python in C++. You could use a stringstream ... #include <sstream> //... std::stringstream ss; for(size_t i = 0; i < v.size(); ++i) { if(i != 0)

How can I convert a stack trace to a string?

。_饼干妹妹 提交于 2019-11-26 01:27:25
问题 What is the easiest way to convert the result of Throwable.getStackTrace() to a string that depicts the stacktrace? 回答1: One can use the following method to convert an Exception stack trace to String . This class is available in Apache commons-lang which is most common dependent library with many popular open sources org.apache.commons.lang.exception.ExceptionUtils.getStackTrace(Throwable) 回答2: Use Throwable.printStackTrace(PrintWriter pw) to send the stack trace to an appropriate writer.