tostring

What's the difference between std::to_string, boost::to_string, and boost::lexical_cast<std::string>?

此生再无相见时 提交于 2019-12-03 09:53:54
What's the purpose of boost::to_string (found in boost/exception/to_string.hpp ) and how does it differ from boost::lexical_cast<std::string> and std::to_string ? std::to_string , available since C++11, works on fundamental numeric types specifically. It also has a std::to_wstring variant. It is designed to produce the same results that sprintf would. You may choose this form to avoid dependencies on external libraries/headers. The throw-on-failure function boost::lexical_cast<std::string> and its non-throwing cousin boost::conversion::try_lexical_convert work on any type that can be inserted

How does getClass in Java work

送分小仙女□ 提交于 2019-12-03 09:40:28
问题 Here is what JavaDoc says: public final Class <?> getClass() Returns the runtime class of this Object . The returned Class object is the object that is locked by static synchronized methods of the represented class. The actual result type is Class<? extends |X|> where |X| is the erasure of the static type of the expression on which getClass is called. For example, no cast is required in this code fragment: Number n = 0; Class<? extends Number> c = n.getClass(); Returns: The Class object that

How do I format a number with commas?

一曲冷凌霜 提交于 2019-12-03 09:36:40
int a = 10000000; a.ToString(); How do I make the output? 10,000,000 Try N0 for no decimal part: string formatted = a.ToString("N0"); // 10,000,000 You can also do String.Format: int x = 100000; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000 If you have decimal, the same code will output 2 decimal places: double x = 100000.2333; string y = string.Empty; y = string.Format("{0:#,##0.##}", x); //Will output: 100,000.23 To make comma instead of decimal use this: double x = 100000.2333; string y = string.Empty; y = string.Format(System.Globalization

Overriding ToString() and adding to ListBox C#

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: Can anyone explain this: public class Test : List <int> { public override string ToString () { return "My ToString" ; } } If I instantiate this and add it to a ListBox control on a Windows Form , it displays "Collection" rather than "My ToString". Test test = new Test (); listBox1 . Items . Add ( test ); I thought the add to Items would just call my class's ToString() . The following works as expected of course MessageBox . Show ( test . ToString ()); 回答1: For that to work you have to disable formatting: listBox1 .

Cannot invoke toString() on the primitive type int

匿名 (未验证) 提交于 2019-12-03 08:41:19
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 问题: Basically, what I'm trying to do, is get the item ID, and set a price from a ini, basically like: itemid:price but, i cannot simply do item.getId().toString(). I'm trying to get item What can I do to make it a string? public static void getBuyPrice(Item item) { try { String itemId = item.getId().toString(); BufferedReader br = new BufferedReader(new FileReader(new File( "./data/prices.ini"))); String line; while ((line = br.readLine()) != null) { if (line.equals(itemId)) { String[] split = line.split(":"); item.getDefinitions().setValue

Do any Java libraries use annotations for code generation?

杀马特。学长 韩版系。学妹 提交于 2019-12-03 07:35:48
Is anyone aware of a library that uses the the techniques (annotations and classworking) described in this article for automatically generating the standard Object methods toString(), equals() and hashcode() for standard java classes? Yes, project Lombok does this. See http://projectlombok.org . It not only supports javac, but also Eclipse. So the methods are not in the source code, but are displayed in the outline view. I certainly haven't seen this and I'm not really sure what value would be gained from it. I find that automatic toString generation is not usually what you want when scouring

How do I format a C# decimal to remove extra following 0's?

扶醉桌前 提交于 2019-12-03 04:29:14
问题 I want to format a string as a decimal, but the decimal contains some following zeros after the decimal. How do I format it such that those meaningless 0's disappear? string.Format("{0}", 1100M); string.Format("{0}", 1100.1M); string.Format("{0}", 1100.100M); string.Format("{0}", 1100.1000M); displays: 1100 1100.1 1100.100 1100.1000 but I want it to be: 1100 1100.1 1100.1 1100.1 For reference, here are other questions that are essentially duplicates of this, that I've found thanks to answers

C# debugging: [DebuggerDisplay] or ToString()?

烂漫一生 提交于 2019-12-03 04:00:39
问题 There are two ways to increase the usefulness of debugging information instead of seeing {MyNamespace.MyProject.MyClass} in the debugger. These are the use of the [DebuggerDisplayAttribute][1] and the ToString() method. using System.Diagnostics; ... [DebuggerDisplay("Name = {Name}")] public class Person { public string Name; } or public class Person { public string Name; public override string ToString() { return string.Format("Name = {0}", Name); } } Is there any reason to prefer one to the

Using Google Guava's Objects.ToStringHelper

陌路散爱 提交于 2019-12-03 03:01:41
问题 I used ToStringBuilder.reflectionToString(class) in commons-lang, to implement toString() for simple DTOs. Now I'm trying to use Google Guava instead of Apache commons library. And I found Objects.ToStringHelper in Guava. But it's too verbose if there're lots of members in the class. For example: @Override public String toString() { return MoreObjects.toStringHelper(this.getClass()).add("name", name) .add("emailAddress", emailAddress) .add("department", department).add("yearJoined",

DateTime ToString(“dd/MM/yyyy”) returns dd.MM.yyyy

匿名 (未验证) 提交于 2019-12-03 02:48:02
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试): 由 翻译 强力驱动 问题: I have also tried shielding the '/' symbol in the formatting string, but it didn't quite work. My final goal is to get the date with the '/' symbols as separators. I guess I can use DateTime.ToString(“dd/MM/yyyy”).Replace('.', '/') , but that feels a bit excessive. 回答1: The / character in date/time format strings stands for "whatever the date separator of the format provider is". Since you do not supply a format provider Thread.CurrentCulture is used, and in your case the current culture uses . as the date separator. If you want to