PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Tuesday, August 9, 2022

[FIXED] How to keep extra decimals in Python

 August 09, 2022     decimal, python     No comments   

Issue

I want to add a number to a numpy array and I would like to keep all the decimals. How can I do it? This is what I tried so far:

import numpy as np
a = np.array([0.25350021,  0.16900018, -0.16899996])
b = 1.05292844e-07
np.around(a+b,decimals=15)

The output is array([ 0.25350032, 0.16900029, -0.16899985]), but b has non-zero digits up to 10^-15 and I would like them to appear explicitly in the numpy array. Thank you!


Solution

I don't know why it is not keeping the 15 digits, however, here is a solution that I came up with to get the answer that you want.

import numpy as np

def addNumberToArray(array, number):
    newArray = []
    for element in array:
        element = element + number
        newArray.append(element)
    return newArray


a = np.array([0.25350021,  0.16900018, -0.16899996])
b = 1.05292844e-07
result = addNumberToArray(a, b)
print (result)

Output: [0.253500315292844, 0.169000285292844, -0.168999854707156]



Answered By - Tanner Stratford
Answer Checked By - Willingham (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing