numeric-conversion

C++ how to set a fixed decimal precision for a float

吃可爱长大的小学妹 提交于 2021-02-08 23:41:45
问题 I have an API call which returns a double. The double's decimal length can variate from many decimal places to a few (it all comes down to the state of an actuator). This double represents the current position on the range radius of an actuator. I am not interested in such a detailed number, because it adds alot of noise to the system. I've been using floats to save space, but still I have floats which have 6-8 decimal length. I am not interested in floor or ceil, simply because it doesn't do

C++ how to set a fixed decimal precision for a float

久未见 提交于 2021-02-08 23:41:05
问题 I have an API call which returns a double. The double's decimal length can variate from many decimal places to a few (it all comes down to the state of an actuator). This double represents the current position on the range radius of an actuator. I am not interested in such a detailed number, because it adds alot of noise to the system. I've been using floats to save space, but still I have floats which have 6-8 decimal length. I am not interested in floor or ceil, simply because it doesn't do

SQL Server CONVERT(NUMERIC(18,0), '') fails but CONVERT(INT, '') succeeds?

岁酱吖の 提交于 2019-12-23 10:48:05
问题 PRINT CONVERT(NUMERIC(18,0), '') produces Error converting data type varchar to numeric. However, PRINT CONVERT(INT, '') produces 0 without error... Question: Is there some SQL Server flag for this or will I need to do case statements for every varchar to numeric conversion? (aside from the obvious why?) 回答1: Use ISNUMERIC declare @a varchar(20) set @a = 'notanumber' select case when isnumeric(@a) = 0 then 0 else convert(numeric(18,0),@a) end 回答2: ISNUMERIC doesn't alway work as you might

How do I convert the value of a TextView to integer

拟墨画扇 提交于 2019-12-11 11:46:22
问题 I am designing a basic BMI calculator and I have the calculator working, but I need to convert the calculated answer from a TextView which is a double to an integer to enable me right statements with <> operators based on the calculated answer. How do I convert the answer from the TextView to an integer? v.findViewById(R.id.bmi).setOnClickListener(this); return v; } public void onClick(View v) { switch (v.getId()) { case R.id.bmi: try { EditText w = (EditText) getView().findViewById(R.id.w);

convert Arabic numerical to English

╄→尐↘猪︶ㄣ 提交于 2019-12-10 17:38:56
问题 i am looking for a way to convert the Arabic numerical string "٠١٢٣٤٥٦٧٨٩" to an English numerical string "0123456789" Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click dim Anum as string ="٠١٢٣٤٥٦٧٨٩" dim Enum as string =get_egnlishNum(Anum) End Sub private function get_egnlishNum(byval _Anum as string) as string '' converting code end function 回答1: You're looking for the GetNumericValue method of the char type which converts any

Best practice in C++ for casting between number types [duplicate]

混江龙づ霸主 提交于 2019-12-10 01:36:56
问题 This question already has answers here : When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? (8 answers) Closed 3 years ago . What is the best practice for casting between the different number types? Types float , double , int are the ones I use the most in C++. An example of the options where f is a float and n is a double or an int : float f = static_cast<float>(n); float f = float(n); float f = (float)n; I usually write static_cast<T>(...) but wondered if there

Best practice in C++ for casting between number types [duplicate]

痴心易碎 提交于 2019-12-05 01:57:53
This question already has an answer here: When should static_cast, dynamic_cast, const_cast and reinterpret_cast be used? 8 answers What is the best practice for casting between the different number types? Types float , double , int are the ones I use the most in C++. An example of the options where f is a float and n is a double or an int : float f = static_cast<float>(n); float f = float(n); float f = (float)n; I usually write static_cast<T>(...) but wondered if there was any consensus within the C++ development community if there is a preferred way. I appreciate this is may end up being an

convert (long mantissa) and (sbyte exponent) to decimal

流过昼夜 提交于 2019-12-01 06:37:10
upd placed my version in the description at the end I need to convert mantissa and exponent to decimal. This is how I coded that: // long field.Decimal.Mantissa // sbyte field.Decimal.Exponent decimal MDEntryPx = field.Decimal.Mantissa * (decimal)(Math.Pow(10, field.Decimal.Exponent)); field.Decimal.Mantissa is integer but Math.Pow(10, field.Decimal.Exponent) is double so I afraid that I can lost precision when casting to decimal. Should I write my own Pow function for integer types that will produce decimal ? What would you suggest? I care about perfomance as I call this functions dozen of

convert (long mantissa) and (sbyte exponent) to decimal

半城伤御伤魂 提交于 2019-12-01 05:05:50
问题 upd placed my version in the description at the end I need to convert mantissa and exponent to decimal. This is how I coded that: // long field.Decimal.Mantissa // sbyte field.Decimal.Exponent decimal MDEntryPx = field.Decimal.Mantissa * (decimal)(Math.Pow(10, field.Decimal.Exponent)); field.Decimal.Mantissa is integer but Math.Pow(10, field.Decimal.Exponent) is double so I afraid that I can lost precision when casting to decimal. Should I write my own Pow function for integer types that will

Is it more efficient to perform a range check by casting to uint instead of checking for negative values?

我与影子孤独终老i 提交于 2019-11-28 19:03:00
I stumbled upon this piece of code in .NET's List source code : // Following trick can reduce the range check by one if ((uint) index >= (uint)_size) { ThrowHelper.ThrowArgumentOutOfRangeException(); } Apparently this is more efficient (?) than if (index < 0 || index >= _size) I am curious about the rationale behind the trick. Is a single branch instruction really more expensive than two conversions to uint ? Or is there some other optimization going on that will make this code faster than an additional numeric comparison? To address the elephant in the room: yes, this is micro optimization,