Convert String into integer value - python-3.x

I need to make following string:
amount = "$163,852.06"
Like this:
16385206000
How can I do it, like I am following this method,:
money = "$163,852.06"
original_amount = ('').join(money[1:].split(','))
print(int(float(original_amount)))
But it is returning me:
163852

>>> int(''.join(c for c in amount if c.isdigit()))*1000
16385206000

It's because you're casting a floating point to an integer value, which rounds to the nearest full number. To achieve the value 16385206000, you could use int(float(original_amount) * 100000).

Related

why is my int() conversion looking so weird?

I'm trying to take a number and divide it by 100 to get 1%, but when i tried to convert it to integer using the int(), it's giving me some weird output. i have no clue what i'm doing wrong here.
totalsupply = 1000000000000000000000000000000
onepercent = int((totalsupply/100))
print(totalsupply)
print(onepercent)
the output is coming out as such:
1000000000000000000000000000000
9999999999999999583119736832
[Finished in 68ms]
I was expecting the onepercent to be this: 10000000000000000000000000000.
According to this post, python tries to convert the number to a float on a division. However, floats are limited to 1.7976931348623157e+308. A workaround is to use the // operator which returns an int from the division, so for your example:
totalsupply = 1000000000000000000000000000000
onepercent = totalsupply//100
print(totalsupply)
print(onepercent)
Python has a built-in integer type that has infinite precision. That's what you are creating with your first line
totalsupply = 1000000000000000000000000000000
However, python has two division operators. The standard division "slash" operator ("/") is floating-point operation, whereas the double-slash gives integers.
Hence totalsupply/100 is a floating point number, which has finite precision, and so the closest it can do is 9999999999999999583119736832.
However, if instead you do
onepercent = totalsupply//100
print(onepercent)
you get the expected 100000000000000000000000000000.

Why Python max() does not return maximum value?

Just First week of learning Python I am trying to print max value from python 3 list. But output is not the max number.
Code:
amount = ['123250','546698','456987','8','1']
print(max(amount))
Output:
8
Screen shot:
Because you are using numbers as strings.
Convert your list to have only integers
amount = ['123250','546698','456987','8','1']
amount = [int(value) for value in amount]
print(max(amount))
This will output
546698
declare the list as a list of ints, not strings ('' is used to wrap around str vars):
amount = [123250,546698,456987,8,1]
print(max(amount))
Convert list of string to list of int using map and use max method.
amount = ['123250','546698','456987','8','1']
print(max(map(int, amount)))
Your list's elements are in string format. You should change it to integer. map() function helps to convert from string format to integer. That's why some functions such as max or sort functions don't work correctly until you convert elements into integer.
amount = ['123250','546698','456987','8','1']
amount=list(map(int,amount))
print(max(amount))

Multiplication in Python not working

I have the bizarre error in python 3.4 where multiplication does not work!
This is my code:
timerlenth = input('Please enter the amount of minute: ')
int(timerlenth)
timersec = (timerlenth*60)
print (timersec)
Here is a photo of the result:
I am practically clueless on trying to solve the problem!
The input function returns a string. Therefore the variable timerlenth stores a string. Next line, int(timerlenth) converts this variable to integer, but does nothing with the result, leaving the timerlenth as the same string it used to be. Python has this functionality where [string]*x will repeat the string x times and that's what you see in the output.
To get actual multiplication, you'll have to store the value of int(timerlenth) to a variable, preferably a new one (good programming practice) and use the new value with multiplication operation.
timerlenth is a string, so the * operator just concatinates it 60 times instead of multiplying it. This caused by your misuse of int - it doesn't change the passed argument, it returns an integer value for it, which you then lose by not assigning it anywhere. Just reassign it to timerlenth and you should be fine:
timerlenth = int(timerlenth)

MATLAB: Convert string to number and then back to string

There is a string containing a number in an arbitrary format (e.g., 12, -34.5, and 6.78e-9). The goal is to convert this string into the corresponding number and then convert this number back to a string such that (a) the precision given in the original string is preserved, and (b) the resulting string has an adequate format (probably, the most adequate format is the format of the original string). I thought the problem could be easily solved using str2num and num2str; however, in some cases, MATLAB seems to be mangling the final result as shown below:
>> a = '1e23'
a =
1e23
>> b = str2num(a)
b =
1.0000e+23
>> c = num2str(b)
c =
9.999999999999999e+22
One solution is to use a general format string:
>> c = num2str(b, '%e')
c =
1.000000e+23
However, in this case, the output looks rather cumbersome for numbers of small orders:
>> d = num2str(1, '%e')
d =
1.000000e+00
In most cases, num2str without additional parameters does a pretty good job resulting in a nicely formatted string. The question is: Is there a way to eliminate the 9.999999999999999e+22 problem?
Thank you!
Regards,
Ivan
In general the representation of one input string does not contain enough information to determine the format. Hence (assuming you want to output slightly different numbers and cannot simply store the number in string format), the simplest way would be to try and find a format that you like.
Judging from your comments, I think you will be happy with:
format short g
For large numbers it will give:
x = num2str(1.0000e+23);str2num(x)
ans =
1e+23
And for small numbers:
x = num2str(1);str2num(x)
ans =
1

Arduino issue: String to float adds two zeros instead of the correct integer

Code snippet:
Serial.println(sensorString); //so you can see the captured string
char carray[sensorString.length() + 1]; //determine size of the array
Serial.println(sizeof(carray));
sensorString.toCharArray(carray, sizeof(carray)); //put sensorString into an array
float sensorStringFloat = atoi(carray); //convert the array into an Integer
Serial.println(sensorStringFloat);
Serial.println(sensorStringFloat) prints out 5.00 instead of the correct float value of 5.33. Why is that and how do I fix this issue? I would eventually like to pass sensorStringFloat over to:
aJson.addNumberToObject(sensor, "ph", sensorStringFloat);
atoi converts a numeral in ASCII to an integer. The comment on that line also says it converts to an integer. So you got an integer result, 5. To convert to floating-point, consider using atof. (Note that “f” stands for floating-point, not “float”. atof returns a double.)
you should pass another parameter which defines the format, in this case it is the number of digits after the floating point.
Serial.println(sensorString,2);
String temp = String (_float, 0);
say float x;
convert to String using
String _temp = String(x, 0);
The second parameter 0... says i want no trailing zeros.
Caution: However this is only suitable for whole numbers.
This solution would not work for say... 1.24
You'll get just 1.

Resources