Round a number in Python
I have a very simple problem. I want to break an interval from 1e-6
to 10e-6
into ten values and append them to a list. For this reason, I made the program below,
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(last_value + step)
last_value = last_value + step
print(current_list)
I get the following result,
[1e-06, 2e-06, 3e-06, 4e-06, 4.9999999999999996e-06, 5.999999999999999e-06, 6.999999999999999e-06, 8e-06, 9e-06, 1e-05].
Of course, the numbers 4.999999999e-6
and 6.999999999e-6
should be 5e-06
and 7e-06
respectively. For this purpose I used the round(**number** ,11)
method.
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(round(last_value + step, 11))
last_value = last_value + step
print(current_list)
Is there another way to make this?
python python-3.x
add a comment |
I have a very simple problem. I want to break an interval from 1e-6
to 10e-6
into ten values and append them to a list. For this reason, I made the program below,
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(last_value + step)
last_value = last_value + step
print(current_list)
I get the following result,
[1e-06, 2e-06, 3e-06, 4e-06, 4.9999999999999996e-06, 5.999999999999999e-06, 6.999999999999999e-06, 8e-06, 9e-06, 1e-05].
Of course, the numbers 4.999999999e-6
and 6.999999999e-6
should be 5e-06
and 7e-06
respectively. For this purpose I used the round(**number** ,11)
method.
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(round(last_value + step, 11))
last_value = last_value + step
print(current_list)
Is there another way to make this?
python python-3.x
2
[round(1e-6 * i, 11) for i in range(1, 11)]
? Ornp.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.
– Amadan
Nov 13 '18 at 11:50
add a comment |
I have a very simple problem. I want to break an interval from 1e-6
to 10e-6
into ten values and append them to a list. For this reason, I made the program below,
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(last_value + step)
last_value = last_value + step
print(current_list)
I get the following result,
[1e-06, 2e-06, 3e-06, 4e-06, 4.9999999999999996e-06, 5.999999999999999e-06, 6.999999999999999e-06, 8e-06, 9e-06, 1e-05].
Of course, the numbers 4.999999999e-6
and 6.999999999e-6
should be 5e-06
and 7e-06
respectively. For this purpose I used the round(**number** ,11)
method.
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(round(last_value + step, 11))
last_value = last_value + step
print(current_list)
Is there another way to make this?
python python-3.x
I have a very simple problem. I want to break an interval from 1e-6
to 10e-6
into ten values and append them to a list. For this reason, I made the program below,
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(last_value + step)
last_value = last_value + step
print(current_list)
I get the following result,
[1e-06, 2e-06, 3e-06, 4e-06, 4.9999999999999996e-06, 5.999999999999999e-06, 6.999999999999999e-06, 8e-06, 9e-06, 1e-05].
Of course, the numbers 4.999999999e-6
and 6.999999999e-6
should be 5e-06
and 7e-06
respectively. For this purpose I used the round(**number** ,11)
method.
start_value = 1e-6
stop_value = 10e-6
step_value = 10
step = (stop_value-start_value)/(step_value-9)
current_list = [start_value]
last_value = start_value
for i in range(step_value-1):
current_list.append(round(last_value + step, 11))
last_value = last_value + step
print(current_list)
Is there another way to make this?
python python-3.x
python python-3.x
edited Nov 13 '18 at 11:46
Cheche
850218
850218
asked Nov 13 '18 at 11:33
Jacob FuchsJacob Fuchs
7610
7610
2
[round(1e-6 * i, 11) for i in range(1, 11)]
? Ornp.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.
– Amadan
Nov 13 '18 at 11:50
add a comment |
2
[round(1e-6 * i, 11) for i in range(1, 11)]
? Ornp.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.
– Amadan
Nov 13 '18 at 11:50
2
2
[round(1e-6 * i, 11) for i in range(1, 11)]
? Or np.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.– Amadan
Nov 13 '18 at 11:50
[round(1e-6 * i, 11) for i in range(1, 11)]
? Or np.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.– Amadan
Nov 13 '18 at 11:50
add a comment |
2 Answers
2
active
oldest
votes
you could use numpy:
import numpy as np
np.arange(1,11,1)*1e-6
output:
array([1.e-06, 2.e-06, 3.e-06, 4.e-06, 5.e-06, 6.e-06, 7.e-06, 8.e-06,
9.e-06, 1.e-05])
add a comment |
You could take a look at the fractions
module:
import fractions
start_value = fractions.Fraction(1, 1000000)
stop_value = fractions.Fraction(10, 1000000)
step_value = 10
step = fractions.Fraction(stop_value-start_value, (step_value - 1))
current_list =
for i in range(step_value):
current_list.append(start_value + i * step)
The default output maintains a list of Fraction
types:
print(current_list)
[Fraction(1, 1000000), Fraction(1, 500000), Fraction(3, 1000000), Fraction(1, 250000), Fraction(1, 200000), Fraction(3, 500000), Fraction(7, 1000000), Fraction(1, 125000), Fraction(9, 1000000), Fraction(1, 100000)]
But it's simple enough to cast to native data types:
print([float(x) for x in current_list])
[1e-06, 2e-06, 3e-06, 4e-06, 5e-06, 6e-06, 7e-06, 8e-06, 9e-06, 1e-05]
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280140%2fround-a-number-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
you could use numpy:
import numpy as np
np.arange(1,11,1)*1e-6
output:
array([1.e-06, 2.e-06, 3.e-06, 4.e-06, 5.e-06, 6.e-06, 7.e-06, 8.e-06,
9.e-06, 1.e-05])
add a comment |
you could use numpy:
import numpy as np
np.arange(1,11,1)*1e-6
output:
array([1.e-06, 2.e-06, 3.e-06, 4.e-06, 5.e-06, 6.e-06, 7.e-06, 8.e-06,
9.e-06, 1.e-05])
add a comment |
you could use numpy:
import numpy as np
np.arange(1,11,1)*1e-6
output:
array([1.e-06, 2.e-06, 3.e-06, 4.e-06, 5.e-06, 6.e-06, 7.e-06, 8.e-06,
9.e-06, 1.e-05])
you could use numpy:
import numpy as np
np.arange(1,11,1)*1e-6
output:
array([1.e-06, 2.e-06, 3.e-06, 4.e-06, 5.e-06, 6.e-06, 7.e-06, 8.e-06,
9.e-06, 1.e-05])
answered Nov 13 '18 at 11:57
user7784503user7784503
474
474
add a comment |
add a comment |
You could take a look at the fractions
module:
import fractions
start_value = fractions.Fraction(1, 1000000)
stop_value = fractions.Fraction(10, 1000000)
step_value = 10
step = fractions.Fraction(stop_value-start_value, (step_value - 1))
current_list =
for i in range(step_value):
current_list.append(start_value + i * step)
The default output maintains a list of Fraction
types:
print(current_list)
[Fraction(1, 1000000), Fraction(1, 500000), Fraction(3, 1000000), Fraction(1, 250000), Fraction(1, 200000), Fraction(3, 500000), Fraction(7, 1000000), Fraction(1, 125000), Fraction(9, 1000000), Fraction(1, 100000)]
But it's simple enough to cast to native data types:
print([float(x) for x in current_list])
[1e-06, 2e-06, 3e-06, 4e-06, 5e-06, 6e-06, 7e-06, 8e-06, 9e-06, 1e-05]
add a comment |
You could take a look at the fractions
module:
import fractions
start_value = fractions.Fraction(1, 1000000)
stop_value = fractions.Fraction(10, 1000000)
step_value = 10
step = fractions.Fraction(stop_value-start_value, (step_value - 1))
current_list =
for i in range(step_value):
current_list.append(start_value + i * step)
The default output maintains a list of Fraction
types:
print(current_list)
[Fraction(1, 1000000), Fraction(1, 500000), Fraction(3, 1000000), Fraction(1, 250000), Fraction(1, 200000), Fraction(3, 500000), Fraction(7, 1000000), Fraction(1, 125000), Fraction(9, 1000000), Fraction(1, 100000)]
But it's simple enough to cast to native data types:
print([float(x) for x in current_list])
[1e-06, 2e-06, 3e-06, 4e-06, 5e-06, 6e-06, 7e-06, 8e-06, 9e-06, 1e-05]
add a comment |
You could take a look at the fractions
module:
import fractions
start_value = fractions.Fraction(1, 1000000)
stop_value = fractions.Fraction(10, 1000000)
step_value = 10
step = fractions.Fraction(stop_value-start_value, (step_value - 1))
current_list =
for i in range(step_value):
current_list.append(start_value + i * step)
The default output maintains a list of Fraction
types:
print(current_list)
[Fraction(1, 1000000), Fraction(1, 500000), Fraction(3, 1000000), Fraction(1, 250000), Fraction(1, 200000), Fraction(3, 500000), Fraction(7, 1000000), Fraction(1, 125000), Fraction(9, 1000000), Fraction(1, 100000)]
But it's simple enough to cast to native data types:
print([float(x) for x in current_list])
[1e-06, 2e-06, 3e-06, 4e-06, 5e-06, 6e-06, 7e-06, 8e-06, 9e-06, 1e-05]
You could take a look at the fractions
module:
import fractions
start_value = fractions.Fraction(1, 1000000)
stop_value = fractions.Fraction(10, 1000000)
step_value = 10
step = fractions.Fraction(stop_value-start_value, (step_value - 1))
current_list =
for i in range(step_value):
current_list.append(start_value + i * step)
The default output maintains a list of Fraction
types:
print(current_list)
[Fraction(1, 1000000), Fraction(1, 500000), Fraction(3, 1000000), Fraction(1, 250000), Fraction(1, 200000), Fraction(3, 500000), Fraction(7, 1000000), Fraction(1, 125000), Fraction(9, 1000000), Fraction(1, 100000)]
But it's simple enough to cast to native data types:
print([float(x) for x in current_list])
[1e-06, 2e-06, 3e-06, 4e-06, 5e-06, 6e-06, 7e-06, 8e-06, 9e-06, 1e-05]
answered Nov 13 '18 at 11:56
DatHydroGuyDatHydroGuy
6662313
6662313
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53280140%2fround-a-number-in-python%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
2
[round(1e-6 * i, 11) for i in range(1, 11)]
? Ornp.linspace(1e-6, 10e-6, 10)
? Despite what Python wants you to think, TIMTOWTDI.– Amadan
Nov 13 '18 at 11:50