tostring

Confused about __str__ on list in Python [duplicate]

夙愿已清 提交于 2019-11-26 18:14:35
This question already has an answer here: Python __str__ and lists 9 answers 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: uno = Node(1) due = Node(2) tri = Node(3) qua = Node(4) Now, the expected behaviour when trying to print one of these

WPF IsEditable=true ComboBox filled with objects displays the ToString() as the selected item

别来无恙 提交于 2019-11-26 17:47:32
问题 The Wpf combo box allows editing, and this is fine if all your combo box items are strings, or have a ToString() method defined on them. When you select an item, it is displayed as Text, it does not use a DataTemplate, it just calls ToString() on the item that is selected. I get a list of items in my combo drop down that are formatted using my item template, when i select one i get the name of the object i.e. MyNamespace.MyObjectName Some solutions have been use ValuePath to bind to a

Override valueof() and toString() in Java enum

旧时模样 提交于 2019-11-26 17:38:15
问题 The values in my enum are words that need to have spaces in them, but enums can't have spaces in their values so it's all bunched up. I want to override toString() to add these spaces where I tell it to. I also want the enum to provide the correct enum when I use valueOf() on the same string that I added the spaces to. For example: public enum RandomEnum { StartHere, StopHere } Call toString() on RandomEnum whose value is StartHere returns string "Start Here" . Call valueof() on that same

toString和toLocalString

我与影子孤独终老i 提交于 2019-11-26 17:13:15
toLocalString()是调用每个数组元素的 toLocaleString() 方法,然后使用地区特定的分隔符把生成的字符串连接起来,形成一个字符串。 toString()方法获取的是String(传统字符串),而toLocaleString()方法获取的是LocaleString(本地环境字符串)。 如果你开发的脚本在世界范围都有人使用,那么将对象转换成字符串时请使用toString()方法来完成。 LocaleString()会根据你机器的本地环境来返回字符串,它和toString()返回的值在不同的本地环境下使用的符号会有微妙的变化。 所以使用toString()是保险的,返回唯一值的方法,它不会因为本地环境的改变而发生变化。如果是为了返回时间类型的数据,推荐使用LocaleString()。若是在后台处理字符串,请务必使用toString()。 来源: https://www.cnblogs.com/jiuxu/p/11328680.html

Why Is ToString() Rounding My Double Value?

旧街凉风 提交于 2019-11-26 16:58:48
问题 How do I prevent my double value from being rounded when converting to a string? I have tried both Convert.ToString and ToString() with the same result. For example my double may look something like 77.987654321 , and the two strings conversions convert to to 77.98765 . I need to keep the precision of the value as is. 回答1: By default the .ToString() method of Double returns 15 digits of precision. If you want the full 17 digits that the double value holds internally, you need to pass the "G17

How to convert double to string without the power to 10 representation (E-05)

不羁岁月 提交于 2019-11-26 16:46:12
问题 How to convert double to string without the power to 10 representation (E-05) double value = 0.000099999999833333343; string text = value.ToString(); Console.WriteLine(text); // 9,99999998333333E-05 I'd like the string text to be 0.000099999999833333343 (or nearly that, I'm not doing rocket science:) I've tried the following variants Console.WriteLine(value.ToString()); // 9,99999998333333E-05 Console.WriteLine(value.ToString("R20")); // 9,9999999833333343E-05 Console.WriteLine(value.ToString

Sorting mixed numbers and strings

不羁岁月 提交于 2019-11-26 16:35:33
问题 I have a list of strings that can contain a letter or a string representation of an int (max 2 digits). They need to be sorted either alphabetically or (when it is actually an int) on the numerical value it represents. Example: IList<string> input = new List<string>() {"a", 1.ToString(), 2.ToString(), "b", 10.ToString()}; input.OrderBy(s=>s) // 1 // 10 // 2 // a // b What I would want is // 1 // 2 // 10 // a // b I have some idea involving formatting it with trying to parse it, then if it is

How does the .ToString() method work?

﹥>﹥吖頭↗ 提交于 2019-11-26 14:47:12
问题 Sometimes when I call a class's .ToString() method, it returns the fully qualified name of the class. But for some class's/struct's (like Int32 ) it returns a string correspoding to the object (value of the integer). Does this mean the Int32 class overrides the ToString() method, and classes that return fully qualified names don't override it, but instead just call base's ( Object 's) ToString() method? Does the Object.ToString() implementation just return the class's fully qualified name?

Why does the default Object.toString() return a hex representation of the hashCode?

爷,独闯天下 提交于 2019-11-26 14:45:56
问题 I'm curious why Object.toString() returns this: return getClass().getName() + "@" + Integer.toHexString(hashCode()); as opposed to this: return getClass().getName() + "@" + hashCode(); What benefits does displaying the hash code as a hex rather than a decimal buy you? 回答1: Object.hashCode used to be computed based on a memory location where the object is located. Memory locations are almost universally displayed as hexadecimal. The default return value of toString isn’t so much interested in

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

微笑、不失礼 提交于 2019-11-26 14:10:29
What is the difference between Convert.ToString() and .ToString() ? I found many differences online, but what's the major difference? Ryan Convert.ToString() handles null , while ToString() doesn't. 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 if it is null. In addition to other answers about handling null values, Convert.ToString tries to use