首页 > 解决方案 > JavaScript SpeechSynthesisUtterance 正确说出数字

问题描述

我将 SpeechSynthesisUtterance 与 JavaScript 和 HTML 一起使用。我想正确地说出数字,所以不要把它读成五三七七九,比如五万三千七百和七十九。那可能吗?我从 localStorage 获取号码。我正在使用这段代码:

var msg = new SpeechSynthesisUtterance("The number is " + localStorage.getItem("mynumber"));
msg.lang = 'en-US';
msg.rate = 4;
window.speechSynthesis.speak(msg);

`

标签: javascriptspeech-synthesis

解决方案


数字的发音方式取决于浏览器和从该浏览器中选择的语音。当我在 Chrome 中尝试时,默认语音说“五十三七十七九”,但是如果我给数字加上千位分隔符(即将它从 53779 更改为 53,779),所有语音都会说“五万三千七百七十九”。

因此尝试添加添加千位分隔符的“.toLocaleString('en')”:

var msg = new SpeechSynthesisUtterance("The number is " + localStorage.getItem("mynumber").toLocaleString('en'));

推荐阅读