Converting seconds (clocks) to decimal format

时间:2015-07-29 00:15:58

标签: c++ time format output

I'm making a stopwatch, and I need to output the seconds out like so: "9.743 seconds".

I have the start time, the end time, and the difference measured out in clocks, and was planning on achieving the decimal by dividing the difference by 1000. However, no matter what I try, it will always output as a whole number. It's probably something small I'm overlooking, but I haven't a clue what. Here's my code:

            var children = ['Kangaroo', 'Alligator', 'Ardvark', 'Ant', 'Bear', 'Bat', 'Cat', 'Cow'];
            children.sort();

            var aZ = children.map(function(c){
                return { name : c};
            });

            var char = '';
            for (i=0; i < aZ.length; i++) {
              var child = aZ[i];  
              var cName = child.name; 
              var cLetter = cName.charAt(0); 
                if(cLetter === undefined || cLetter !== char){
                    if(cLetter)
                         document.write('</ul>'); 
                    document.write('<h1>' + cLetter +'</h1><ul>'); 
                    char = cLetter;
                }
                document.write('<li>' + cName +'</li>');    
            }

Like I said, the output is integer. Say it timed 5892 clocks: the output would be "5".

2 个答案:

答案 0 :(得分:2)

Division between integers is still an integer. Cast one of your division parameters to a real type (double or float) and assign to another variable that is a real type.

recipe_ingredients

答案 1 :(得分:1)

devcron

it will give you real number output