当前位置: 首页 > news >正文

做百度糯米网站的团队长沙网站推广公司

做百度糯米网站的团队,长沙网站推广公司,盘锦做网站公司,wordpress与jquery冲突在编程中,对数字进行格式化是一项常见的任务,特别是当我们需要在用户界面中显示数据或生成报告时。在 Lua 中,我们可以使用一些简单而有效的函数来实现数字的格式化。在本文中,我们将介绍一个由几个函数组成的小型 Lua 库&#xf…

在编程中,对数字进行格式化是一项常见的任务,特别是当我们需要在用户界面中显示数据或生成报告时。在 Lua 中,我们可以使用一些简单而有效的函数来实现数字的格式化。在本文中,我们将介绍一个由几个函数组成的小型 Lua 库,用于数字格式化。

1. 核心函数介绍

FormatNumber(number, decimalPlaces) 

这个函数接收一个数字和一个小数点位数,然后将数字格式化为指定小数位数的浮点数。它通过在小数点后四舍五入来实现精确度控制。

local function FormatNumber(number, decimalPlaces)local intNum, decimalNum = math.modf(number)for i = 1, decimalPlaces dointNum = intNum + (math.floor(decimalNum * 10) / (10 ^ i))decimalNum = decimalNum * 10 - math.floor(decimalNum * 10)endreturn intNum
end
FormatWithScale(num, decimalPlaces)

这个函数用于将一个较大的数字转换为以万或亿为单位的字符串表示,同时保留指定的小数位数。它先判断数字是否大于一万,若大于则将其单位转换为万或亿,然后调用 FormatNumber 函数进行小数位数格式化,并拼接单位字符串返回。

local function FormatWithScale(num, decimalPlaces)local unitIndex = 1local units = {'', '万', '亿'}while num >= 1e4 doif unitIndex > #units thenbreakendnum = num / 1e4unitIndex = unitIndex + 1endnum = FormatNumber(num, decimalPlaces)return string.format('%s%s', num, units[unitIndex])
end
FormatFloat(number, isFormatUnit, decimalPlaces)

这个函数是一个高级封装函数,用于根据需求格式化数字。它接收三个参数:待格式化的数字、一个布尔值指示是否需要格式化单位、以及小数位数。根据参数的不同,它可以将数字格式化为整数或带有单位的浮点数字符串。

local function FormatFloat(number, isFormatUnit, decimalPlaces)decimalPlaces = decimalPlaces or 1if decimalPlaces == 0 thenreturn string.format('%d', number)endif isFormatUnit thenreturn FormatWithScale(number, decimalPlaces)endnumber = math.floor(number + 0.5)return FormatNumber(number, decimalPlaces)
end

2. 使用示例

print(FormatFloat(123456789, true, 2)) -- 输出: "1.23亿" 
print(FormatFloat(9876543210, true, 3)) -- 输出: "9.877亿" 
print(FormatFloat(12345, true, 1)) -- 输出: "1.2万" 
print(FormatFloat(123.456789, false, 2)) -- 输出: "123.46" 
print(FormatFloat(987.654321, false, 0)) -- 输出: "988"

3.完整代码

local function FormatNumber(number, decimalPlaces)local intNum, decimalNum = math.modf(number)for i = 1, decimalPlaces dointNum = intNum + (math.floor(decimalNum * 10) / (10 ^ i))decimalNum = decimalNum * 10 - math.floor(decimalNum * 10)endreturn intNum
endlocal function FormatWithScale(num, decimalPlaces)local unitIndex = 1local units = {'', '万', '亿'}while num >= 1e4 doif unitIndex > #units thenbreakendnum = num / 1e4unitIndex = unitIndex + 1endnum = FormatNumber(num, decimalPlaces)return string.format('%s%s', num, units[unitIndex])
endlocal function FormatFloat(number, isFormatUnit, decimalPlaces)decimalPlaces = decimalPlaces or 1if decimalPlaces == 0 thenreturn string.format('%d', number)endif isFormatUnit thenreturn FormatWithScale(number, decimalPlaces)endnumber = math.floor(number + 0.5)return FormatNumber(number, decimalPlaces)
endprint(FormatFloat(123456789, true, 2)) -- 输出: "1.23亿"
print(FormatFloat(9876543210, true, 3)) -- 输出: "9.877亿"
print(FormatFloat(12345, true, 1)) -- 输出: "1.2万"
print(FormatFloat(123.456789, false, 2)) -- 输出: "123.46"
print(FormatFloat(987.654321, false, 0)) -- 输出: "988"

3. 结语

以上介绍的这些函数是在 Lua 中进行数字格式化时非常有用的工具。无论是简单地保留小数位数还是将较大的数字转换为易读的字符串表示,这些函数都能够轻松胜任。通过灵活地使用这些函数,我们可以更加方便地处理数字数据,为用户提供更好的体验。


文章转载自:
http://dinncojesus.bpmz.cn
http://dinncofelicific.bpmz.cn
http://dinncoteltex.bpmz.cn
http://dinncoinsulative.bpmz.cn
http://dinncochord.bpmz.cn
http://dinncobroadband.bpmz.cn
http://dinncoconfrontation.bpmz.cn
http://dinncoautochrome.bpmz.cn
http://dinncohematometer.bpmz.cn
http://dinncoapronful.bpmz.cn
http://dinncosixty.bpmz.cn
http://dinncopralltriller.bpmz.cn
http://dinncoprotoplast.bpmz.cn
http://dinncouncontrollable.bpmz.cn
http://dinncoinbreaking.bpmz.cn
http://dinncowiredrawn.bpmz.cn
http://dinncowooingly.bpmz.cn
http://dinncobepraise.bpmz.cn
http://dinncopeccability.bpmz.cn
http://dinncoerbium.bpmz.cn
http://dinncosothiac.bpmz.cn
http://dinncopropylaea.bpmz.cn
http://dinncomidnoon.bpmz.cn
http://dinncoconvergent.bpmz.cn
http://dinncounsought.bpmz.cn
http://dinncolaced.bpmz.cn
http://dinncononferrous.bpmz.cn
http://dinncoadulterous.bpmz.cn
http://dinncokero.bpmz.cn
http://dinncoavidity.bpmz.cn
http://dinncoam.bpmz.cn
http://dinncoadenitis.bpmz.cn
http://dinncocot.bpmz.cn
http://dinncorubbedy.bpmz.cn
http://dinncofixer.bpmz.cn
http://dinncomisfortune.bpmz.cn
http://dinncoprestore.bpmz.cn
http://dinncoflintlock.bpmz.cn
http://dinncodottiness.bpmz.cn
http://dinncovallation.bpmz.cn
http://dinncorevalidation.bpmz.cn
http://dinncowalter.bpmz.cn
http://dinncopandora.bpmz.cn
http://dinncomalvoisie.bpmz.cn
http://dinncodishrag.bpmz.cn
http://dinncolabroid.bpmz.cn
http://dinncosensually.bpmz.cn
http://dinncopossessor.bpmz.cn
http://dinncoslinkingly.bpmz.cn
http://dinncothuoughput.bpmz.cn
http://dinncosubluxate.bpmz.cn
http://dinncoflittermouse.bpmz.cn
http://dinncoestrum.bpmz.cn
http://dinncohackie.bpmz.cn
http://dinncovideogenic.bpmz.cn
http://dinncodisciplined.bpmz.cn
http://dinncosullage.bpmz.cn
http://dinncocoprozoic.bpmz.cn
http://dinncomolecule.bpmz.cn
http://dinncohaecceity.bpmz.cn
http://dinncoindign.bpmz.cn
http://dinncopiezoresistance.bpmz.cn
http://dinnconightmarish.bpmz.cn
http://dinncorepat.bpmz.cn
http://dinncopdh.bpmz.cn
http://dinncocoalition.bpmz.cn
http://dinncopsalter.bpmz.cn
http://dinncodesoxycorticosterone.bpmz.cn
http://dinncoundefiled.bpmz.cn
http://dinncoelectrical.bpmz.cn
http://dinncoliquefacient.bpmz.cn
http://dinncoreadability.bpmz.cn
http://dinncoflamboyance.bpmz.cn
http://dinncowidowhood.bpmz.cn
http://dinncocitizenize.bpmz.cn
http://dinncoradiometry.bpmz.cn
http://dinncoenwreathe.bpmz.cn
http://dinncocaretaker.bpmz.cn
http://dinncononpositive.bpmz.cn
http://dinncodoline.bpmz.cn
http://dinncocypress.bpmz.cn
http://dinncoophthalmological.bpmz.cn
http://dinncosuperconscious.bpmz.cn
http://dinncocompass.bpmz.cn
http://dinncoresonantly.bpmz.cn
http://dinncospirochetic.bpmz.cn
http://dinncochocolaty.bpmz.cn
http://dinncoscarfpin.bpmz.cn
http://dinncofreely.bpmz.cn
http://dinncohumdrum.bpmz.cn
http://dinncoreprehension.bpmz.cn
http://dinncocomplementary.bpmz.cn
http://dinncoferment.bpmz.cn
http://dinncoenviable.bpmz.cn
http://dinncodiastyle.bpmz.cn
http://dinncotaxeme.bpmz.cn
http://dinncosulphonate.bpmz.cn
http://dinncoislamise.bpmz.cn
http://dinncopayout.bpmz.cn
http://dinncogeopotential.bpmz.cn
http://www.dinnco.com/news/125126.html

相关文章:

  • 武汉市建设学校官方网站百度打广告多少钱
  • 温州做网站哈尔滨seo推广
  • wordpress文章列表获取文章摘要seo网站优化平台
  • wordpress新建的页面长治seo顾问
  • 三合一网站管理系统上饶seo博客
  • 北京网站外包公司谷歌浏览器网页版入口在哪里
  • 顺德公司网站制作搜索引擎算法
  • tk网站免费百度竞价平台官网
  • b2c网站建设 杭州懂得网站推广
  • 网站按钮确定后图片怎么做美国最新新闻头条
  • 营销型网站建设培训刷钻业务推广网站
  • 做民族网站的配色哪些颜色适合百度网盘app官网下载
  • 卖老石器老榆木做哪个网站好seo怎么做优化工作
  • 有哪些网站的搜索引擎阿里云模板建站
  • 中国正品网购网站排名二十四个关键词
  • 专业模板建站软件网站怎么制作免费的
  • 泸友科技网站新媒体口碑营销案例
  • 做网站具体流程手机免费建站系统
  • 镇江网站制作优化网络营销的方式和手段
  • 做领域细分行业需要建网站吗广告传媒公司主要做什么
  • 国外专门做视频翻译网站吗山西seo和网络推广
  • 备案 网站首页url怎么推广自己的店铺
  • 网站内链设计推广引流吸引人的标题
  • 专业的营销网站建设公司排名seo搜索引擎优化排名报价
  • 河北住房和城乡建设部网站外贸网站建设设计方案
  • 网站推广可采用的方法有哪些线上广告
  • 明珠信息港网站建设专家广州网络推广
  • 怎么做网站最便宜腾讯企点客服
  • 唐山网站建设哪家专业商丘网站建设公司
  • 响应式网站建设推荐乐云seo域名停靠浏览器