negative-number

Python format negative currency

那年仲夏 提交于 2019-12-10 16:08:42
问题 I haven't found anything that addresses how to format negative currency, so far, and it is driving me crazy. from decimal import * import re import sys import os import locale locale.setlocale( locale.LC_ALL, 'English_United States.1252' ) # cBalance is a running balance of type Decimal fBalance = locale.currency( cBalance, grouping=True ) print cBalance, fBalance Result with Negative Number: -496.06 ($496.06) I need a minus sign NOT parenthesis How do I get rid of the parenthesis and get

Prevent user passing negative numbers to a function accepting unsigned int

孤街醉人 提交于 2019-12-10 15:34:52
问题 So here's the code: int create_mask(unsigned b, unsigned e) { unsigned int mask=1; if(b<e || b<0 || e<0) { printf("Wrong values, starting bit can't be smaller than ending.\n"); printf("Both got to be >= 0.\n"); exit(EXIT_FAILURE); } while(b>0) { printf("%u\n", b); mask<<=1; if(b>e) mask|=1; b--; } return ~mask; /* negates mask for later purpose that is clearing corresponding bits */ } Function creates mask for some bit operations, but should take two unsigned ints b and e, both non-negative.

Compilers and negative numbers representations

雨燕双飞 提交于 2019-12-10 14:23:14
问题 Recently I was confused by this question. Maybe because I didn't read language specifications (it's my fault, I know). C99 standard doesn't say which negative numbers representation should be used by compiler. I always thought that the only right way to store negative numbers is two's complement (in most cases). So here's my question: do you know any present-day compiler that implements by default one's complement or sign-magnitude representation? Can we change default representation with

How does Verilog behave with negative numbers?

六眼飞鱼酱① 提交于 2019-12-08 14:55:52
问题 For instance, say I have a reg [7:0] myReg I assign it the value -8'D69 I know Verilog stores it as 2's complement so it should be stored as 10111011 The question I have now is if I were to perform an operation on it, say myReg/2 Would it evaluate to -34? Or would it take 10111011 and turn it into 187 then perform the division, returning 93? 回答1: You need to remember that -8d69 is just a bit pattern. reg is a type which holds bit patterns. It is the type of variable that instructs / to

How to calculate modulo of negative integers in JavaScript?

本秂侑毒 提交于 2019-12-08 14:45:59
问题 I'm trying to iterate over an array of jQuery objects, by incrementing or decrementing by 1. So, for the decrementing part, I use this code: var splitted_id = currentDiv.attr('id').split('_'); var indexOfDivToGo = parseInt(splitted_id[1]); indexOfDivToGo = (indexOfDivToGo-1) % allDivs.length; var divToGo = allDivs[indexOfDivToGo]; so I have 4 elements with id's: div_0 div_1 div_2 div_3 I was expecting it to iterate as 3 - 2 - 1 - 0 - 3 - 2 - etc.. but it returns -1 after the zero, therefore

Using 'Greater than' operator with a negative number

谁都会走 提交于 2019-12-08 07:14:54
问题 I need to run several queries against columns containing both positive and negative numbers and return all rows that are either < or > than a selected value, however it's not returning the expected results when I use a 'Greater than' operator if the selected value is a negative number. Please note that when the selected value is a negative number it should be returning both positive and negative numbers, and it seems to be excluding the negative numbers from the results. SELECT T3.* FROM

Using 'Greater than' operator with a negative number

别等时光非礼了梦想. 提交于 2019-12-07 00:35:30
I need to run several queries against columns containing both positive and negative numbers and return all rows that are either < or > than a selected value, however it's not returning the expected results when I use a 'Greater than' operator if the selected value is a negative number. Please note that when the selected value is a negative number it should be returning both positive and negative numbers, and it seems to be excluding the negative numbers from the results. SELECT T3.* FROM Rules T3 WHERE T3.Width > '-.80'; The values contained in T3.Width are: 0.90,(0.70),(0.70),(0.70),(0.70),(1

PHP Unsigned Right Shift - Malfunctioning

…衆ロ難τιáo~ 提交于 2019-12-05 11:37:07
So, when using my method to preform a ( >>> ) unsigned right shift in PHP, the result is incorrect when the numbers involve negatives. PHP Application Results: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 2684354558 JAVA APPLICATION RESULTS: INPUT: 10 >>> 3 INPUT: -10 >>> 3 OUTPUT: 1 OUTPUT: 536870910 (The top results are correct and generated by Java and then the bottom results are incorrect and generated by PHP) It's only when the number is negative in PHP that it fails. The shifts being used in those applications is: Please help if you can! Method for shifting in PHP: function urshift

Why does right shifting -1 always gives -1 in PHP?

萝らか妹 提交于 2019-12-05 07:46:08
I am trying to figure out why if I shift the negative integer -1 I always get -1, e.g.: echo -1 >> 64; // -1 echo -1 >> 5; // -1 echo -1 >> 43; // -1 echo -1 >> 1; // -1 Whatever second operand of the right shift is given, -1 remains -1... I do understand that when you perform a right shift you're actually doing this: x >> y = x / 2^y But in the case of x being -1, if so, I do: -1 >> 3 = -1 / 2^3 Shouldn't this value be -1/8 = -0.125? Thanks for the attention. Bitwise shift operators don't divide. They do what they are supposed to do - shift bits. In particular, the right shift operator does

Unsigned negative primitives?

主宰稳场 提交于 2019-12-05 07:07:30
In C++ we can make primitives unsigned . But they are always positive. Is there also a way to make unsigned negative variables? I know the word unsigned means "without sign", so also not a minus (-) sign. But I think C++ must provide it. No. unsigned can only contain nonnegative numbers. If you need a type that only represent negative numbers, you need to write a class yourself, or just interpret the value as negative in your program. (But why do you need such a type?) unsigned integers are only positive. From 3.9.1 paragraph 3 of the 2003 C++ standard: The range of nonnegative values of a