摘錄自
http://www.hkln.net/_discuss/discuss.pl ... g&thid=134
<script>
var v = '12.34567';
alert( roundp(v, 3) );
/*
Round a string to some decimal places
*/
function roundp(v, dp) {
var dotpos; // position of decimal point
var ary = new Array();
// Make sure it has a dot
if ( v.indexOf('.') == -1 ) v += '.';
dotpos = v.indexOf(".");
// Convert string to Array
for (var i = 0 ; i < v.length ; i++) ary = v.charAt(i);
// Append trailing zero if the string is too short
var temp = dotpos + dp - v.length + 1;
for (var i = 0 ; i <= temp ; i++) ary[ary.length] = '0';
// Check if carry is needed after rounding
var carry_flag = false;
if ( ary[dotpos + dp + 1] >= 5 ) carry_flag = true;
// Do carry for other digit
for (var i = dotpos + dp ; i >= 0 && carry_flag ; i--) {
// Skip the dot and pocess the next digit
if (ary == '.') continue;
if (ary != '9') {
ary++;
carry_flag = false;
} else { // 9 + 1 = 10
ary = '0';
carry_flag = true;
}
}
// Check if a new digit is needed after carry
v = (carry_flag)? '1' : '';
// Chop the last digit
ary.length = dotpos + dp + 1;
// Chop the dot if no decimal place
if (dp == 0) ary.length--;
// Convert array into string
for (var i = 0 ; i < ary.length ; i++) v = v + ary;
return v;
}
</script>
請問javascript 裡如何取道小數點第二位呢
前往
- Software
- ↳ CodeCharge Studio
- ↳ CodeCharge
- ↳ DemoCharge
- ↳ SuperPDF
- ↳ 551einv
- ↳ E3進銷存
- 程式語言
- ↳ PHP
- ↳ CodeLobster PHP Edition
- ↳ Yii
- ↳ CodeIgniter
- ↳ Phalcon
- ↳ Symfony
- ↳ FuelPHP
- ↳ Zend Framework 2
- ↳ laravel
- ↳ WordPress
- ↳ ASP.NET/C#
- ↳ ASP/VBScript
- ↳ JSP
- ↳ Java Servlets
- ↳ ColdFusion
- ↳ Perl
- ↳ Java Script
- ↳ jQuery
- ↳ HTML + CSS
- ↳ jQuery
- ↳ nodejs
- ↳ VB6
- ↳ Git
- ↳ App Inventor 2
- ↳ bash
- ↳ C++/ VC/ OpenCV
- ↳ OpenCV
- ↳ go
- ↳ cordova
- ↳ python
- ↳ Xamarin
- ↳ Assembly
- 資料庫
- ↳ MySQL
- ↳ PostgreSQL
- ↳ ORACLE
- ↳ Access
- ↳ SQL Server
- ↳ SQLite
- ↳ MariaDB
- ↳ Mongodb
- 作業系統
- ↳ Linux
- ↳ Ubuntu
- ↳ CentOS
- ↳ Mint
- ↳ Mandriva
- ↳ Debian
- ↳ Red Hat Enterprise Linux
- ↳ Oracle Linux
- ↳ Fedora
- ↳ Kali Linux
- ↳ OpenSUSE
- ↳ Elementary OS
- ↳ Microsoft
- ↳ Server 2008 R2
- ↳ Server 2012 R2
- ↳ Server 2012
- ↳ 8
- ↳ 10
- ↳ System Center 2016
- ↳ NOVELL
- ↳ FreeBSD
- ↳ VMware
- ↳ VirtualBox
- ↳ Mac OS X
- ↳ Solaris
- ↳ iOS
- ↳ Android
- ↳ Cloud
- ↳ OpenStack
- ↳ Docker
- ↳ Proxmox VE
- ↳ CloudReady
- ↳ chrome
- 網頁伺服器
- ↳ apache
- ↳ tomcat
- ↳ nginx
- ↳ IIS
- ↳ JBoss
- ↳ weblogic
- ↳ WebHosting
- 硬體
- ↳ 硬體及週邊
- ↳ RouterOS
- ↳ LEGO NXT
- ↳ Arduino
- ↳ MSP430
- ↳ Raspberry Pi
- ↳ OpenERP
- ↳ Storage
- ↳ Server
- ↳ Brocade
- ↳ MODELS
- ↳ FortiGate
- 軟體
- ↳ sublime
- ↳ LibreNMS