Posts

Showing posts with the label Type Conversion

Convert INT To VARCHAR SQL

Answer : Use the convert function. SELECT CONVERT(varchar(10), field_name) FROM table_name Use the STR function: SELECT STR(field_name) FROM table_name Arguments float_expression Is an expression of approximate numeric (float) data type with a decimal point. length Is the total length. This includes decimal point, sign, digits, and spaces. The default is 10. decimal Is the number of places to the right of the decimal point. decimal must be less than or equal to 16. If decimal is more than 16 then the result is truncated to sixteen places to the right of the decimal point. source: https://msdn.microsoft.com/en-us/library/ms189527.aspx You can use CAST function: SELECT CAST(your_column_name AS varchar(10)) FROM your_table_name

Convert String To Enum In Python

Answer : This functionality is already built in to Enum [1]: >>> from enum import Enum >>> class Build(Enum): ... debug = 200 ... build = 400 ... >>> Build['debug'] <Build.debug: 200> [1] Official docs: Enum programmatic access Another alternative (especially useful if your strings don't map 1-1 to your enum cases) is to add a staticmethod to your Enum , e.g.: class QuestionType(enum.Enum): MULTI_SELECT = "multi" SINGLE_SELECT = "single" @staticmethod def from_str(label): if label in ('single', 'singleSelect'): return QuestionType.SINGLE_SELECT elif label in ('multi', 'multiSelect'): return QuestionType.MULTI_SELECT else: raise NotImplementedError Then you can do question_type = QuestionType.from_str('singleSelect') def custom_enum(typename, items_dict): class_definition = """ from...

Convert Float To String In Php?

Answer : echo number_format($float,0,'.',''); note: this is for integers, increase 0 for extra fractional digits $float = 0.123; $string = sprintf("%.3f", $float); // $string = "0.123"; It turns out json_decode by default casts large integers as floats. This option can be overwritten in the function call: $json_array = json_decode($json_string, , , 1); I'm basing this only on the main documentation, so please test and let me know if it works.

Convert Bytes To Int?

Answer : Assuming you're on at least 3.2, there's a built in for this: int.from_bytes ( bytes, byteorder, *, signed=False ) ... The argument bytes must either be a bytes-like object or an iterable producing bytes. The byteorder argument determines the byte order used to represent the integer. If byteorder is "big", the most significant byte is at the beginning of the byte array. If byteorder is "little", the most significant byte is at the end of the byte array. To request the native byte order of the host system, use sys.byteorder as the byte order value. The signed argument indicates whether two’s complement is used to represent the integer. ## Examples: int.from_bytes(b'\x00\x01', "big") # 1 int.from_bytes(b'\x00\x01', "little") # 256 int.from_bytes(b'\x00\x10', byteorder='little') # 4096 int.from_bytes(b'\xfc...

Converting A String To A Date In JavaScript

Answer : The best string format for string parsing is the date ISO format together with the JavaScript Date object constructor. Examples of ISO format: YYYY-MM-DD or YYYY-MM-DDTHH:MM:SS . But wait! Just using the "ISO format" doesn't work reliably by itself. String are sometimes parsed as UTC and sometimes as localtime (based on browser vendor and version). The best practice should always be to store dates as UTC and make computations as UTC. To parse a date as UTC, append a Z - e.g.: new Date('2011-04-11T10:20:30Z') . To display a date in UTC, use .toUTCString() , to display a date in user's local time, use .toString() . More info on MDN | Date and this answer. For old Internet Explorer compatibility (IE versions less than 9 do not support ISO format in Date constructor), you should split datetime string representation to it's parts and then you can use constructor using datetime parts, e.g.: new Date('2011', '04' - 1, '11'...

Convert RGBA To RGB In Python

Answer : You probably want to use an image's convert method: import PIL.Image rgba_image = PIL.Image.open(path_to_image) rgb_image = rgba_image.convert('RGB') In case of numpy array, I use this solution: def rgba2rgb( rgba, background=(255,255,255) ): row, col, ch = rgba.shape if ch == 3: return rgba assert ch == 4, 'RGBA image has 4 channels.' rgb = np.zeros( (row, col, 3), dtype='float32' ) r, g, b, a = rgba[:,:,0], rgba[:,:,1], rgba[:,:,2], rgba[:,:,3] a = np.asarray( a, dtype='float32' ) / 255.0 R, G, B = background rgb[:,:,0] = r * a + (1.0 - a) * R rgb[:,:,1] = g * a + (1.0 - a) * G rgb[:,:,2] = b * a + (1.0 - a) * B return np.asarray( rgb, dtype='uint8' ) in which the argument rgba is a numpy array of type uint8 with 4 channels. The output is a numpy array with 3 channels of type uint8 . This array is easy to do I/O with library imageio using imread and imsave .