以下代码用于将任何数字从1-999转换为单词。但是,该代码不会将其转换为除100的倍数以外的任何3位数字。例如,它的版画567为“五百七十七”。该程序还可以打印1到100之间的所有数字。请告知。谢谢!
ctrl.activities = [
{
"Completed": false,
"Notes": "Pork loin strip steak andouille, kevin cow porchetta spare ribs rump. Leberkas capicola jerky cow. Shank pork loin bacon fatback boudin t-bone flank. Porchetta filet mignon brisket, pork loin boudin short loin burgdoggen chuck beef short ribs fatback ham chicken prosciutto biltong. Shoulder buffalo pork andouille, doner ground round sausage porchetta chicken beef ribs spare ribs. Prosciutto bresaola doner ham bacon drumstick ground round shankle kielbasa. 1"
},
{
"Completed": false,
"Notes": "followup future"
},
{
"Completed": false,
"Notes": "dac"
},
{
"Completed": false,
"Notes": "dac"
},
{
"Completed": false,
"Notes": "Spare ribs cupim turducken pastrami. Alcatra ground round venison jowl chuck meatball turducken hamburger shoulder fatback frankfurter tenderloin ham. Short ribs spare ribs pig flank, frankfurter turkey biltong pork chop hamburger alcatra ball tip. Turkey filet mignon cupim shankle sirloin kielbasa brisket pork fatback ham pig turducken jerky bacon."
}
];
el.tabulator({
layout: 'fitColumns'
, responsiveLayout: 'hide'
, placeholder: 'No activities'
, data: ctrl.activities
, columns: [{{
title: 'Notes'
, field: 'Notes'
, minWidth: 100
, cellDblTap: notesClick
, cellClick: notesClick
}, {
title: 'Toggle'
, headerSort: false
, field: 'Completed'
}]
function notesClick(e, cell) {
$log.debug({type: 'tabulator cell click', e, cell});
tabActivities.tabulator('deleteColumn', 'Notes');
tabActivities.tabulator('addColumn', {
title: 'Notes'
, field: 'Notes'
, formatter: 'textarea'
, variableHeight: true
, cellDblTap: expandedNotesClick
, cellTap: expandedNotesClick
, cellClick: expandedNotesClick
}, true, 'Completed');
}
function expandedNotesClick(e, cell) {
$log.debug({type: 'tabulator cell click', e, cell});
tabActivities.tabulator('deleteColumn', 'Notes');
tabActivities.tabulator('addColumn', {
title: 'Notes'
, field: 'Notes'
, minWidth: 100
, cellDblTap: notesClick
, cellTap: notesClick
, cellClick: notesClick
}, true, 'Completed');
}
答案 0 :(得分:1)
在您的if
语句中输入100到999之间的数字,
tensDigit = Math.floor(num / 10);
但是,对于数字567,Math.floor(num / 10);
会跳到56
,这是您的tens()
函数中未包含的数字。要解决此问题,tensDigit
应该设置为
tensDigit = Math.floor(num / 10) % 10;
答案 1 :(得分:1)
很抱歉指出这一点,但是您的代码很长且没有算法。这是工作的:
package assortie
{
import flash.display.Sprite;
public class SpellNumber extends Sprite
{
// Class constructor and tests.
public function SpellNumber()
{
super();
trace(toWords(567));
trace(toWords(7));
trace(toWords(56));
trace(toWords(19));
trace(toWords(913));
}
static private const ONES :Array = ["", "One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine"];
static private const TENS :Array = ["", "", "Twenty", "Thirty", "Forty", "Fifty", "Sixty", "Seventy", "Eighty", "Ninety"];
static private const TEENS:Array = ["Ten", "Eleven", "Twelve", "Thirteen", "Fourteen", "Fifteen", "Sixteen", "Seventeen", "Eighteen", "Nineteen"];
// This method converts numbers fom 1 to 999 to words.
static private function toWords(value:int):String
{
var result:String = "";
// Unexpected data cases.
if (value < 1) return "Number is out of bounds: " + value + " is zero or less.";
if (value > 999) return "Number is out of bounds: " + value + " is thousand or greater.";
// Figure out hundreds.
var aHun:int = value / 100;
if (aHun > 0)
{
// Remove hundreds from the given value.
value %= 100;
// Form the words for hundreds.
result += ONES[aHun] + " Hundred";
}
// Check if rest is teens.
if ((value > 9) && (value < 20))
{
// Add one space if result is not empty.
if (result) result += " ";
// Form the words for teens.
result += TEENS[value - 10];
// Return the result. There's nothing to do here any longer.
return result;
}
// Figure out tens.
var aTens:int = value / 10;
if (aTens > 0)
{
// Remove tens from the rest of the value.
value %= 10;
// Add one space if result is not empty.
if (result) result += " ";
// Form the words for tens.
result += TENS[aTens];
}
// Figure out the last digit.
if (value > 0)
{
// Add one space if result is not empty.
if (result) result += " ";
// Form the words for the last digit.
result += ONES[value];
}
return result;
}
}
}
答案 2 :(得分:0)
在这种情况下此行的问题(num> = 100 && num <= 999)
tensDigit = Math.floor(num / 10);
此行的结果是11或12或13,等等。因此,将无法满足tens()函数的条件。
将此行修改为:
tensDigit = Math.floor(num % 100 / 10);
告诉我是否可行。