decimal

Convert Binary to Decimal in Objective C

我们两清 提交于 2019-12-19 21:25:36
问题 I have binary sequence in a string. How to convert it in decimal. Is there any built in function in Objective C ? 回答1: NSString * b = @"1101"; long v = strtol([b UTF8String], NULL, 2); NSLog(@"%ld", v); //logs 13 The downside of this is that it only appears to produce positive numbers. 回答2: I couldn't find any built in function. So wrote simple C function according to the process we do for conversation. (int) BinToInt(char *temp) { int count, total, i, j, tmp; total = 0; count = strlen(temp);

Convert Binary to Decimal in Objective C

陌路散爱 提交于 2019-12-19 21:24:24
问题 I have binary sequence in a string. How to convert it in decimal. Is there any built in function in Objective C ? 回答1: NSString * b = @"1101"; long v = strtol([b UTF8String], NULL, 2); NSLog(@"%ld", v); //logs 13 The downside of this is that it only appears to produce positive numbers. 回答2: I couldn't find any built in function. So wrote simple C function according to the process we do for conversation. (int) BinToInt(char *temp) { int count, total, i, j, tmp; total = 0; count = strlen(temp);

Easiest way to truncate float to 2 decimal places?

二次信任 提交于 2019-12-19 17:41:34
问题 In Swift, is there a way to truncate a float to 2 decimals, such that you can perform further calculations with it? All of the threads I've seen deal with casting to a string, which I can't figure out how to then use mathematically. I tried using an extension (found on this forum), figuring I could cast back to float after the truncation, but I end up where I started, with another, non-truncated float. I need my return value to be in quarter steps (i.e. 6.50, 6.75, 5.25, etc), and what I'm

Dividing decimals yields invalid results in Python 2.5 to 2.7

删除回忆录丶 提交于 2019-12-19 12:53:37
问题 After a very thorough read of the Python's decimal module documentation, I still find myself puzzled by what happens when I divide a decimal. In Python 2.4.6 (makes sense): >>> import decimal >>> decimal.Decimal(1000) / 10 Decimal("100") In Python 2.5.6, Python 2.6.7, and Python 2.7.2 (puzzling): >>> import decimal >>> decimal.Decimal(1000) / 10 Decimal('0.00000-6930898827444486144') More confusing yet, that result doesn't even appear to be valid: >>> decimal.Decimal('0.00000

What does a plus sign do in front of a variable in Python?

本秂侑毒 提交于 2019-12-19 12:29:21
问题 There's the following bit of Python code in a project I have to maintain: # If the `factor` decimal is given, compute new price and a delta factor = +factor.quantize(TWOPLACES) new_price = +Decimal(old_price * factor).quantize(TWOPLACES) delta = new_price - old_price The question here is the purpose of + in front of a variable. Python docs call it unary plus operator , which “yields its numeric argument unchanged”. Can it be safely removed then? (Incidentally, the code was written by me some

What does a plus sign do in front of a variable in Python?

怎甘沉沦 提交于 2019-12-19 12:29:00
问题 There's the following bit of Python code in a project I have to maintain: # If the `factor` decimal is given, compute new price and a delta factor = +factor.quantize(TWOPLACES) new_price = +Decimal(old_price * factor).quantize(TWOPLACES) delta = new_price - old_price The question here is the purpose of + in front of a variable. Python docs call it unary plus operator , which “yields its numeric argument unchanged”. Can it be safely removed then? (Incidentally, the code was written by me some

How do I use basic batch math commands to come up with decimal answers?

依然范特西╮ 提交于 2019-12-19 11:35:09
问题 I am making a batch file so that I just tell it what kind of formula I need to use and it tells me what variables I need to input. Right now, I am coding it so that it will find the area of a triangle. I have written the code so that it asks you the base, height, and the units of the triangle. As you should know, the formula is simply, base x height / 2. So I tested the code to see if it works. But I noticed that when it divides an odd number by 2, it doesn't give a decimal answer. It instead

Convert base-27 (or base-X) to base-10 in c#?

岁酱吖の 提交于 2019-12-19 08:10:08
问题 Is there a ready made function to be able to do base conversions in c#? I am looking to convert from base 26 and base base 27 numbers to base 10. I can do it on paper but i am not a very experienced programmer and would rather not do it from scratch if possible. Thanks! 回答1: There is a ready-made function to convert numbers from base 2, 8 or 16 to base 10 ( Convert.ToInt32). If you want to convert numbers from base 26 or base 27 to base 10, you'll have to do it yourself. Now, I've never heard

Convert percent value to decimal value in PHP

寵の児 提交于 2019-12-19 05:44:14
问题 How to convert percent value to decimal value in PHP to be used in a computation? Example: 6.15% = .0615 回答1: $dec = $pct / 100.00; // if you actually have a % sign in $pct strip it out $pct = '15%'; $dec = str_replace('%', '', $pct) / 100.00; 回答2: echo floatval("6.15%") / 100.00; 回答3: function percentToDecimal($percent): float { $percent = str_replace('%', '', $percent); return $percent / 100.00; } 来源: https://stackoverflow.com/questions/2389182/convert-percent-value-to-decimal-value-in-php

What does M,D mean in decimal(M,D) exactly?

瘦欲@ 提交于 2019-12-19 05:37:28
问题 Does anyone know about this? 回答1: As the docs say: M is the maximum number of digits (the precision). It has a range of 1 to 65. (Older versions of MySQL allowed a range of 1 to 254.) D is the number of digits to the right of the decimal point (the scale). It has a range of 0 to 30 and must be no larger than M. So M stands for Maximum (number of digits overall), D stands for Decimals (number of digits to the right of the decimal point). 回答2: https://dev.mysql.com/doc/refman/5.7/en/precision