- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
假设我们有一个数组 [0.09, 870, 499],我们想要获取数组值:[0.1, 1000, 100]?
我尝试过什么:
var logarithmicRound = function(val) {
var degree = Math.round(Math.log(val) / Math.LN10);
if(Math.pow(10, degree) - val > val) {
--degree;
}
return Math.pow(10, degree);
};
console.log(logarithmicRound(0.05));
console.log(logarithmicRound(0.7));
console.log(logarithmicRound(49));
console.log(logarithmicRound(50));
console.log(logarithmicRound(400));
console.log(logarithmicRound(800));
// prints
//0.1
//1
//10
//100
//100
//1000
然而它看起来很丑......但它正是我所需要的。
最佳答案
我使用了几个函数来对数字进行四舍五入,它们可能很有用。
function roundTo2(value){
return (Math.round(value * 100) / 100);
}
function roundResult(value, places){
var multiplier = Math.pow(10, places);
return (Math.round(value * multiplier) / multiplier);
}
您显然需要对数字进行舍入并放入数组/提取、舍入、放回 - 不如其他人的答案那么有效
关于javascript - 如何使javascript对值的位数敏感?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17831597/
我这里有一些(遗留)代码,似乎在LD_LIBRARY_PATH上调用setenv(其值在编译时未知,实际上它将从命令中获取)行),现在我必须将其移植到 Windows。我怀疑 setenv 只是出于历
我在 SSIS 包上有一个敏感字符串参数,用于存储远程服务器的密码。 但是,当字符串值包含花括号时,作业代理会在配置该步骤的包参数时抛出错误: Microsoft SQL Server Managem
我们都非常了解 strictfp 的工作原理。 像这样: package com.hk.basicjava.tests.strictfp; import java.util.*; public cla
我正在对我的博客进行简单搜索。我使用亚美尼亚语,当我搜索时,这些字母总是很敏感。这是我的代码的一部分。提前谢谢你。 search_query = get.get('search') query_lis
我正在对我的博客进行简单搜索。我使用亚美尼亚语,当我搜索时,这些字母总是很敏感。这是我的代码的一部分。提前谢谢你。 search_query = get.get('search') query_lis
想象一下这个非常基本的可拖放设置: #dropArea 是可放置的。 #itemBox > .item 是可拖动对象。 由于某种原因,droppable
我有这样的网址: http://quickstart.local/public/category1/product2 并且在 url (category1/product2) 数字是 id ,从数据库
我是一名优秀的程序员,十分优秀!