How to round numbers to the nearest 1000?
I'm having some issue to round up and down of a list of number to the nearest 1000.
Below is my code:
rev_list =
for i in range(12):
rev = int(round(random.normalvariate(100000, 12000)))
rev_list.append(rev)
print(rev_list)
The output is:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
I would like to round the list to the nearest 1000. How can I do that?
python rounding
add a comment |
I'm having some issue to round up and down of a list of number to the nearest 1000.
Below is my code:
rev_list =
for i in range(12):
rev = int(round(random.normalvariate(100000, 12000)))
rev_list.append(rev)
print(rev_list)
The output is:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
I would like to round the list to the nearest 1000. How can I do that?
python rounding
add a comment |
I'm having some issue to round up and down of a list of number to the nearest 1000.
Below is my code:
rev_list =
for i in range(12):
rev = int(round(random.normalvariate(100000, 12000)))
rev_list.append(rev)
print(rev_list)
The output is:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
I would like to round the list to the nearest 1000. How can I do that?
python rounding
I'm having some issue to round up and down of a list of number to the nearest 1000.
Below is my code:
rev_list =
for i in range(12):
rev = int(round(random.normalvariate(100000, 12000)))
rev_list.append(rev)
print(rev_list)
The output is:
[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
I would like to round the list to the nearest 1000. How can I do that?
python rounding
python rounding
edited Nov 13 '18 at 20:02
wim
161k50308440
161k50308440
asked Sep 29 '17 at 3:24
123cremepie123cremepie
394
394
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
Note: My example behavior is the Python 3 behavior forround
; if you're on Python 2, the results would befloat
values, but wrapping inint()
would convert back toint
.
– ShadowRanger
Sep 29 '17 at 3:32
add a comment |
List comprehension is a one-line loop which allows you to apply a function to the list items. (for more read List Comprehensions)
[x for x in rev_list]
In this case, round(num, -3) is the function.
>>> round(1300,-3)
1000
>>>
The answer
You can apply a function on a list by this code
rev_list=[round(x,-3) for x in rev_list]
The example:
>>> rev_list=[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
>>> rev_list=[round(x,-3) for x in rev_list]
>>> rev_list
[97000, 96000, 105000, 132000, 98000, 88000, 85000, 95000, 95000, 90000, 93000, 86000]
>>>
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
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%2f46481351%2fhow-to-round-numbers-to-the-nearest-1000%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
The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
Note: My example behavior is the Python 3 behavior forround
; if you're on Python 2, the results would befloat
values, but wrapping inint()
would convert back toint
.
– ShadowRanger
Sep 29 '17 at 3:32
add a comment |
The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
Note: My example behavior is the Python 3 behavior forround
; if you're on Python 2, the results would befloat
values, but wrapping inint()
would convert back toint
.
– ShadowRanger
Sep 29 '17 at 3:32
add a comment |
The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
The round
function can take negative digits to round to, which causes it to round off to the left of the decimal. For example:
>>> round(15768, -3)
16000
>>> round(1218, -3)
1000
So the short answer is: Call round
with the second argument of -3
to round to the nearest 1000.
answered Sep 29 '17 at 3:28
ShadowRangerShadowRanger
60.1k55796
60.1k55796
Note: My example behavior is the Python 3 behavior forround
; if you're on Python 2, the results would befloat
values, but wrapping inint()
would convert back toint
.
– ShadowRanger
Sep 29 '17 at 3:32
add a comment |
Note: My example behavior is the Python 3 behavior forround
; if you're on Python 2, the results would befloat
values, but wrapping inint()
would convert back toint
.
– ShadowRanger
Sep 29 '17 at 3:32
Note: My example behavior is the Python 3 behavior for
round
; if you're on Python 2, the results would be float
values, but wrapping in int()
would convert back to int
.– ShadowRanger
Sep 29 '17 at 3:32
Note: My example behavior is the Python 3 behavior for
round
; if you're on Python 2, the results would be float
values, but wrapping in int()
would convert back to int
.– ShadowRanger
Sep 29 '17 at 3:32
add a comment |
List comprehension is a one-line loop which allows you to apply a function to the list items. (for more read List Comprehensions)
[x for x in rev_list]
In this case, round(num, -3) is the function.
>>> round(1300,-3)
1000
>>>
The answer
You can apply a function on a list by this code
rev_list=[round(x,-3) for x in rev_list]
The example:
>>> rev_list=[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
>>> rev_list=[round(x,-3) for x in rev_list]
>>> rev_list
[97000, 96000, 105000, 132000, 98000, 88000, 85000, 95000, 95000, 90000, 93000, 86000]
>>>
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
add a comment |
List comprehension is a one-line loop which allows you to apply a function to the list items. (for more read List Comprehensions)
[x for x in rev_list]
In this case, round(num, -3) is the function.
>>> round(1300,-3)
1000
>>>
The answer
You can apply a function on a list by this code
rev_list=[round(x,-3) for x in rev_list]
The example:
>>> rev_list=[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
>>> rev_list=[round(x,-3) for x in rev_list]
>>> rev_list
[97000, 96000, 105000, 132000, 98000, 88000, 85000, 95000, 95000, 90000, 93000, 86000]
>>>
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
add a comment |
List comprehension is a one-line loop which allows you to apply a function to the list items. (for more read List Comprehensions)
[x for x in rev_list]
In this case, round(num, -3) is the function.
>>> round(1300,-3)
1000
>>>
The answer
You can apply a function on a list by this code
rev_list=[round(x,-3) for x in rev_list]
The example:
>>> rev_list=[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
>>> rev_list=[round(x,-3) for x in rev_list]
>>> rev_list
[97000, 96000, 105000, 132000, 98000, 88000, 85000, 95000, 95000, 90000, 93000, 86000]
>>>
List comprehension is a one-line loop which allows you to apply a function to the list items. (for more read List Comprehensions)
[x for x in rev_list]
In this case, round(num, -3) is the function.
>>> round(1300,-3)
1000
>>>
The answer
You can apply a function on a list by this code
rev_list=[round(x,-3) for x in rev_list]
The example:
>>> rev_list=[97277, 96494, 104541, 132060, 98179, 87862, 84718, 95391, 94674, 89773, 92790, 86122]
>>> rev_list=[round(x,-3) for x in rev_list]
>>> rev_list
[97000, 96000, 105000, 132000, 98000, 88000, 85000, 95000, 95000, 90000, 93000, 86000]
>>>
edited Dec 30 '18 at 18:35
answered Dec 30 '18 at 3:32
Reza energyReza energy
195
195
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
add a comment |
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
5
5
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
Usually it's better to explain a solution instead of just posting some rows of anonymous code. You can read How do I write a good answer, and also Explaining entirely code-based answers
– Anh Pham
Dec 30 '18 at 4:02
2
2
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
While this code may answer the question, it is better to explain how to solve the problem and provide the code as an example or reference. Code-only answers can be confusing and lack context.
– Dima Kozhevin
Dec 30 '18 at 7:04
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
@AnhPham, thank you for your comment. done
– Reza energy
Dec 30 '18 at 18:37
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%2f46481351%2fhow-to-round-numbers-to-the-nearest-1000%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