casting

Is it possible to cast with precision in java without using the formatted printf? [duplicate]

有些话、适合烂在心里 提交于 2020-06-13 01:53:07
问题 This question already has answers here : printf %f with only 2 numbers after the decimal point? (8 answers) Closed 3 months ago . This line results in double value 3.33333333335 System.out.println("Average marks of " + name + " = " + (double)sum/3); Is it possible to set a width of precision? 回答1: You can use DecimalFormat or BigDecimal as follows: import java.math.BigDecimal; import java.math.RoundingMode; import java.text.DecimalFormat; public class Main { public static void main(String[]

Is reinterpret_cast type punning actually undefined behavior?

北城以北 提交于 2020-06-10 04:38:10
问题 It appears to be widely-held that type punning via reinterpret_cast is somehow prohibited (properly: "undefined behavior", that is, "behavior for which this International Standard imposes no requirements", with an explicit note that implementations may define behavior) in C++. Am I incorrect in using the following reasoning to disagree, and if so, why ? [expr.reinterpret.cast]/11 states: A glvalue expression of type T1 can be cast to the type “reference to T2 ” if an expression of type

Casting to Decimal is not supported in LINQ to Entities queries

只谈情不闲聊 提交于 2020-06-10 02:54:27
问题 I have a database table Transaction (transactionID, LocalAmount...). where datatype for Localamount property is float . ON the UI I am trying to return a SUM of column (Localamount) in one row on a button click event. I have used decimal instead of float However I am getting an error on the code where I am casting to decimal System.NotSupportedException was unhandled by user code Message=Casting to Decimal is not supported in LINQ to Entities queries, because the required precision and scale

Incompatible pointer to integer conversion assigning to 'u32' (aka 'unsigned int') from 'struct net *'

喜夏-厌秋 提交于 2020-06-01 06:57:08
问题 What I want: To add a network namespace option to execsnoop bcc tool to trace only the logs with specified network namespace just like we have filter PID option in many other bcc tools. For eg: execsnoop -N "ns_id" I am using linux kernel structures to retrieve namespace id net = task->nsproxy->net_ns; and need to assign the retrieved ns to data.netns which is u32 int. What I am doing: int syscall__execve(struct pt_regs *ctx, const char __user *filename, const char __user *const __user *_

MISRA C-2012 Rule 11.3 violation while trying to do a typecast from char to int pointer

跟風遠走 提交于 2020-05-30 07:53:23
问题 I am trying to get rid of Rule 11.3 from my code. Sample code: static int32_t do_test(const char *cp) { const char *c = cp; const int32_t *x; x = (const int32_t *)cp; return *x; } I want the value of *c and *x to be same. Even-though the code is compiling and giving the correct value, "x = (int32_t *)cp;" causing violation of 11.3 and 11.8 Rule 11.3 violation: An object with pointer type shall not be converted to a pointer to a different object type. I have tried with void pointer, but the

Dart typecasting error

天涯浪子 提交于 2020-05-29 02:39:31
问题 I have an abstract class Event and a concrete subclass that extends it called PlaceChangeEvent . Inside an event listener, I have the following code: void onEvent(Event event) { PlaceChangeEvent pce = null; if(event is PlaceChangeEvent) pce = (PlaceChangeEvent)event; // <== error is here else throw new Exception("Event is not a PlaceChangeEvent."); Place place = pce.place; presenterProvider.display(place); } So if the runtime type of event is PlaceChangeEvent , then I need to cast the event

Dart typecasting error

ぐ巨炮叔叔 提交于 2020-05-29 02:39:13
问题 I have an abstract class Event and a concrete subclass that extends it called PlaceChangeEvent . Inside an event listener, I have the following code: void onEvent(Event event) { PlaceChangeEvent pce = null; if(event is PlaceChangeEvent) pce = (PlaceChangeEvent)event; // <== error is here else throw new Exception("Event is not a PlaceChangeEvent."); Place place = pce.place; presenterProvider.display(place); } So if the runtime type of event is PlaceChangeEvent , then I need to cast the event

Casting arrays of a supertype to a subtype

强颜欢笑 提交于 2020-05-26 07:38:56
问题 Is there a particular reason why this results in runtime exception instead of compile-time error in Java? Object[] objects = new Object[10]; String[] strings = (String[])objects; 回答1: The check has to be done at run time because of this case: public class Test { public static void main(String[] args){ String[] stringsBase = {"aaa", "bbb", "ccc"}; Object[] objects = stringsBase; String[] strings = (String[])objects; System.out.println(strings[1]); } } This is a valid, working program. Without

Casting arrays of a supertype to a subtype

六月ゝ 毕业季﹏ 提交于 2020-05-26 07:38:23
问题 Is there a particular reason why this results in runtime exception instead of compile-time error in Java? Object[] objects = new Object[10]; String[] strings = (String[])objects; 回答1: The check has to be done at run time because of this case: public class Test { public static void main(String[] args){ String[] stringsBase = {"aaa", "bbb", "ccc"}; Object[] objects = stringsBase; String[] strings = (String[])objects; System.out.println(strings[1]); } } This is a valid, working program. Without

Casting arrays of a supertype to a subtype

本秂侑毒 提交于 2020-05-26 07:37:47
问题 Is there a particular reason why this results in runtime exception instead of compile-time error in Java? Object[] objects = new Object[10]; String[] strings = (String[])objects; 回答1: The check has to be done at run time because of this case: public class Test { public static void main(String[] args){ String[] stringsBase = {"aaa", "bbb", "ccc"}; Object[] objects = stringsBase; String[] strings = (String[])objects; System.out.println(strings[1]); } } This is a valid, working program. Without