casting

In Java, can I consolidate two similar functions where uses JspWriter and the other PrintWriter?

走远了吗. 提交于 2019-12-12 10:47:48
问题 I have the following class, which as you will see has rather a rather redundant formatNameAndAddress method: package hu.flux.helper; import java.io.PrintWriter; import javax.servlet.jsp.JspWriter; // A holder for formatting data public class NameAndAddress { public String firstName; public String middleName; public String lastName; public String address1; public String address2; public String city; public String state; public String zip; // Print out the name and address. public void

Convert double to int in C++ without round down errors

回眸只為那壹抹淺笑 提交于 2019-12-12 10:39:01
问题 I have the following codes to cast a double into an int : double dblValue = 7.30; int intValue = (int)(dblValue*100); //I want intValue to store extactly 730; cout << intValue; Output: 729 I know that the compiler is reading dblValue as 7.2999999 before casting it to int. My question is : Is it possible to cast it as 730 by preventing the round down errors? It would be best if your solution avoid using C++11 or other predefined functions. The only pre-processor directive I am using here is

Cant cast float to int if object

左心房为你撑大大i 提交于 2019-12-12 10:38:18
问题 This code runs fine float ff = 5.5f; int fd = (int) ff; Console.Write(fd); Where as this code doesnt float ff = 5.5f; object jf = ff; int fd = (int) jf; Console.Write(fd); What rule in the runner causes this to happen? 回答1: You can cast a float to an int, but you can't cast a boxed float to an int - you have to unbox it first. int fd = (int)(float)jf; Read Eric Lippert's post Representation and Identity for more details. 回答2: float ff = 5.5f; object jf = ff; int fd = (int) jf; here when you

Java Ternary Operator seems to be inconsistantly casting Integers to ints

依然范特西╮ 提交于 2019-12-12 10:34:26
问题 One of my students is getting a null pointer exception when using a ternary operator which sometimes results in null. I think I understand the issue, but it seems like it's resulting from an inconsistent type inference. Or to put another way, I feel like the semantics here are inconsistent and the error should be avoidable without changing his approach. This question is similar to, but different from Another question about ternary operators. In that question, the null Integer must be forced

TypeScript: Convert a bool to string value

送分小仙女□ 提交于 2019-12-12 10:32:39
问题 I have a really simple issue, I can't get to convert a simple boolean to a string value in TypeScript. I have been roaming throught documentation and I could not find anything helpful and of course I tried to use the toString() method but it does not seem to be implemented on bool. Edit: I have almost no JavaScript knowledge and came to TypeScript with a C#/Java background. 回答1: This is either a bug in TypeScript or a concious design decision, but you can work around it using: var myBool:

Safe to cast long double to uint64_t

大憨熊 提交于 2019-12-12 10:24:59
问题 I'm using std::hash<std::string>() to create a hash code for a string in C++. The function returns a long double but I need a uint64_t for inherited reasons. Is such a cast safe? 回答1: C++ Standard paragraph 3.9.1.8 says: There are three floating point types: float, double, and long double. The type double provides at least as much precision as float, and the type long double provides at least as much precision as double. The set of values of the type float is a subset of the set of values of

Failing in trying to code smart cast template function

假如想象 提交于 2019-12-12 10:21:06
问题 I am currently trying to code a function to perform static_cast or dynamic_cast based on its input and output types in following of DRY principle. The function which i am trying to accomplish is as below: #define where typename = typename #define can_cast(FROM, TO) std::is_convertible<FROM, TO>::value #define can_dynamic_cast(FROM, TO) \ can_cast(FROM, TO) && \ !std::is_same<FROM, TO>::value && \ std::is_class<TO>::value && \ !std::is_const<FROM>::value && \ std::is_base_of<TO, FROM>::value

Convert an int column to a varchar in FrontBase SQL?

萝らか妹 提交于 2019-12-12 10:16:28
问题 How do I convert an int column to a varchar in FrontBase SQL ? I need to concatenate a varchar and an int field, and I cannot just concatenate them because both arguments need to be a varchar. I wanted to tag this "Frontbase" but I don't have permission due to reputation < 1500. Server and database version is 4.2.7 The query looks like this: select (TVERKOPEN.CPREFIX || CAST(TVERKOPEN.CNUMMER AS VARCHAR)) as PurchaseOrder from TVERKOPEN 回答1: Try with: CAST(field AS VARCHAR(30)) 来源: https:/

c++ Trouble casting overloaded function: <unresolved overloaded function type>

感情迁移 提交于 2019-12-12 09:53:26
问题 I have functions in a superclass designed to associate a string with internal functions: class Base { typedef std::function<void(double)> double_v; bool registerInput(std::string const& key, double_v const& input) { functions[key] = input; } void setInput(std::string key, double value) { auto fit = functions.find(key); if (fit == functions.end()) return; fit->second(value); } std::map<std::string, double_v> functions; } The idea being that any subclass I can register functions can call them

Treat a single integer value as a range in Swift

泪湿孤枕 提交于 2019-12-12 09:53:17
问题 I need to validate the length of a string. The allowed values for the character count are: 6 – 9 characters 12 characters 15 characters All strings with a different character count are invalid. Thus, I would like to create a Swift function that accepts a number of ranges and evaluates the string: extension String { func evaluateLength(validCharacterCounts: Range<Int>...) -> Bool { // Implementation } } Now I can call the function for a single Int range: "Live long and prosper".evaluateLength