casting

Converting an integer to a string in PHP

≡放荡痞女 提交于 2019-12-17 03:21:58
问题 Is there a way to convert an integer to a string in PHP? 回答1: You can use the strval() function to convert a number to a string. From a maintenance perspective its obvious what you are trying to do rather than some of the other more esoteric answers. Of course, it depends on your context. $var = 5; // Inline variable parsing echo "I'd like {$var} waffles"; // = "I'd like 5 waffles // String concatenation echo "I'd like ".$var." waffles"; // I'd like 5 waffles // Explicit cast $items = (string

How do you emulate nominal typing in TypeScript?

倾然丶 夕夏残阳落幕 提交于 2019-12-17 03:20:56
问题 I have many functions in my project that take an number as parameter; half the time this number is an index into an array, the other half of the time, it is a cursor position (a point between two entries in an array). This causes confusion, even with naming conventions. I would like to enforce that the functions below are taking the intended nominal types. class Index extends Number {} class CursorPosition extends Number {} function getElement(i: Index) {} function getRange(p1: CursorPosition

What exactly is a type cast in C/C++?

跟風遠走 提交于 2019-12-17 03:05:09
问题 What exactly is a type cast in C/C++? How does the compiler check if an explicit typecast is needed (and valid)? Does it compare the space required for an value? If I have for example: int a; double b = 15.0; a = (int) b; If I remember correctly a double value requires more space (was it 8 bytes?!) than an integer (4 bytes). And the internal representation of both are completely different (complement on two/mantissa). So what happens internally? The example here is quite straightforward, but

explicit type casting example in java

假如想象 提交于 2019-12-17 03:00:19
问题 I have come across this example on http://www.javabeginner.com/learn-java/java-object-typecasting and in the part where it talks about explicit type casting there is one example which confuses me. The example: class Vehicle { String name; Vehicle() { name = "Vehicle"; } } class HeavyVehicle extends Vehicle { HeavyVehicle() { name = "HeavyVehicle"; } } class Truck extends HeavyVehicle { Truck() { name = "Truck"; } } class LightVehicle extends Vehicle { LightVehicle() { name = "LightVehicle"; }

Fastest way to convert string to integer in PHP

◇◆丶佛笑我妖孽 提交于 2019-12-17 02:53:09
问题 Using PHP, what's the fastest way to convert a string like this: "123" to an integer? Why is that particular method the fastest? What happens if it gets unexpected input, such as "hello" or an array? 回答1: I've just set up a quick benchmarking exercise: Function time to run 1 million iterations -------------------------------------------- (int) "123": 0.55029 intval("123"): 1.0115 (183%) (int) "0": 0.42461 intval("0"): 0.95683 (225%) (int) int: 0.1502 intval(int): 0.65716 (438%) (int) array("a

Fastest way to convert a dict's keys & values from `unicode` to `str`?

£可爱£侵袭症+ 提交于 2019-12-17 02:27:04
问题 I'm receiving a dict from one "layer" of code upon which some calculations/modifications are performed before passing it onto another "layer". The original dict's keys & "string" values are unicode , but the layer they're being passed onto only accepts str . This is going to be called often, so I'd like to know what would be the fastest way to convert something like: { u'spam': u'eggs', u'foo': True, u'bar': { u'baz': 97 } } ...to: { 'spam': 'eggs', 'foo': True, 'bar': { 'baz': 97 } } ..

How to cast a double to an int in Java by rounding it down?

情到浓时终转凉″ 提交于 2019-12-17 02:23:14
问题 I need to cast a double to an int in Java, but the numerical value must always round down. i.e. 99.99999999 -> 99 回答1: Casting to an int implicitly drops any decimal. No need to call Math.floor() (assuming positive numbers) Simply typecast with (int), e.g.: System.out.println((int)(99.9999)); // Prints 99 This being said, it does have a different behavior from Math.floor which rounds towards negative infinity (@Chris Wong) 回答2: To cast a double to an int and have it be rounded to the nearest

Casting a result to float in method returning float changes result

試著忘記壹切 提交于 2019-12-17 02:11:37
问题 Why does this code print False in .NET 4? It seems some unexpected behavior is being caused by the explicit cast. I'd like an answer beyond "floating point is inaccurate" or "don't do that". float a(float x, float y) { return ( x * y ); } float b(float x, float y) { return (float)( x * y ); } void Main() { Console.WriteLine( a( 10f, 1f/10f ) == b( 10f, 1f/10f ) ); } PS: This code came from a unit test, not release code. The code was written this way deliberately. I suspected it would fail

Converting byte array to string in javascript

谁说胖子不能爱 提交于 2019-12-17 01:45:33
问题 How do I convert a byte array into a string? I have found these functions that do the reverse: function string2Bin(s) { var b = new Array(); var last = s.length; for (var i = 0; i < last; i++) { var d = s.charCodeAt(i); if (d < 128) b[i] = dec2Bin(d); else { var c = s.charAt(i); alert(c + ' is NOT an ASCII character'); b[i] = -1; } } return b; } function dec2Bin(d) { var b = ''; for (var i = 0; i < 8; i++) { b = (d%2) + b; d = Math.floor(d/2); } return b; } But how do I get the functions

Shorter syntax for casting from a List<X> to a List<Y>?

穿精又带淫゛_ 提交于 2019-12-17 01:41:12
问题 I know its possible to cast a list of items from one type to another (given that your object has a public static explicit operator method to do the casting) one at a time as follows: List<Y> ListOfY = new List<Y>(); foreach(X x in ListOfX) ListOfY.Add((Y)x); But is it not possible to cast the entire list at one time? For example, ListOfY = (List<Y>)ListOfX; 回答1: If X can really be cast to Y you should be able to use List<Y> listOfY = listOfX.Cast<Y>().ToList(); Some things to be aware of (H/T