C# convert int to English word

时间:2015-05-12 23:14:21

标签: c#

I would like to convert an integer into a word in the form of:

  • 1 => First
  • 2 => Second
  • 3 => Third

etc.

I have tried googling and have found answers on how to convert ints to plain english words like one, two, three etc. Does anyone know if there are any methods out there that do this already? I'd like to avoid writing my own if possible but I think that's what it will come to.

2 个答案:

答案 0 :(得分:3)

No, a mapping like that does not currently exist in C# (apart from any third party libraries of course).

Easy enough to do though:

function column_cb($item)
    {
        return sprintf(
            '<input type="checkbox" name="id[]" value="%s" />',
            $item['id']
        );
    }    

function get_bulk_actions()
        {
            $actions = array(
                'delete' => 'Delete'
            );
            return $actions;
        }


    function process_bulk_action()
            {
                global $wpdb;
                $bp_table_name = $wpdb->prefix . 'custom_table'; // do not forget about tables prefix

        ?>
                <script type="text/javascript">

              jQuery(document).ready(function() {

                jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action']");

                jQuery('<option>').val('export').text('<?php _e('Export')?>').appendTo("select[name='action2']");

              });

            </script>
                <?php
                if ('export' === $this->current_action()) {
                    ob_end_clean();
                    $sql = $wpdb->get_results( "SELECT * FROM $bp_table_name");

                    if (!$sql) {
                        die('Invalid query: ' . mysql_error());
                    }

                    // Get The Field Name
                    $output .= 'Exhibitor Company'. ',';
                    $output .= 'Exhibitor Representative'. ',';
                    $output .= 'Position Title'. ',';
                    $output .= 'Mailing Address'. ',';
                    $output .= 'City'. ',';
                    $output .= 'State'. ',';
                    $output .= 'Zip'. ',';
                    $output .= 'Website'. ',';
                    $output .= 'Office Phone'. ',';
                    $output .= 'Cell Phone'. ',';
                    $output .= 'Email'. ',';
                    $output .= 'Backup Email'. ',';
                    $output .= 'Products/Services'. ',';
                    $output .= 'Same Booth Space'. ',';
                    $output .= 'Booth Numbers'. ',';
                    $output .= 'Convention Center'. ',';
                    $output .= 'Expo Halls'. ',';

                    $output .="\n";

                    // Get Records from the table

                    foreach ($sql as $row) {

                    $output .="\n";
                    $output .='"'.$row->exhibitor_company.'",';
                    $output .='"'.$row->exhibitor_rep.'",';
                    $output .='"'.$row->title.'",';
                    $output .='"'.$row->address.'",';
                    $output .='"'.$row->city.'",';
                    $output .='"'.$row->state.'",';
                    $output .='"'.$row->zip.'",';
                    $output .='"'.$row->website.'",';
                    $output .='"'.$row->office_phone.'",';
                    $output .='"'.$row->cell.'",';
                    $output .='"'.$row->email.'",';
                    $output .='"'.$row->backup_email.'",';
                    $output .='"'.$row->products_services.'",';
                    $output .='"'.$row->same_space.'",';
                    $output .='"'.$row->booth_numbers.'",';
                    $output .='"'.$row->convention_center.'",';
                    $output .='"'.$row->expo_halls.'",';

                    }
                    $output .="\n";
                    // Download the file

                    $file = "custom_table";
                    $filename = $file."_".date("Y-m-d_H-i",time());
                    header("Content-type: application/vnd.ms-excel");
                    header("Content-disposition: csv" . date("Y-m-d") . ".csv");
                    header( "Content-disposition: filename=".$filename.".csv");

                    echo $output;
                    exit;

                }

                $entry_id = ( is_array( $_REQUEST['id'] ) ) ? $_REQUEST['id'] : array( $_REQUEST['id'] );

                if ( 'delete' === $this->current_action() ) {
                    global $wpdb;

                    foreach ( $entry_id as $id ) {
                        $id = absint( $id );
                        $wpdb->query( "DELETE FROM $bp_table_name WHERE id = $id" );
                    }
                }
            }

答案 1 :(得分:0)

If you're open to using a third-party library, you can use Humanizer. Sample usage from the link:

class EditGroupPage 
  include PageObject
  CHECKBOX_COL = 0
  div(:otherStudents, :id => 'other-students')
  #div(:studentList) {:otherStudents_element.div_element(:index => 1) }    #fails
  #div(:studentList) {:otherStudents_element.div(:index => 1) }  #fails
  #div(:studentList) {otherStudents_element.div(:index => 1) }  #fails
div(:studentList){otherStudents_element.div_element(:index => 1) }  #table-wrapper div
table(:students){studentList_element.table_element}  #doesn't quite fail, but has a warning that table can't be used like that.
#table(:students, studentList_element.table)   #fails
#divs(:allDivs) = otherStudents_element.div_elements
checkbox(:selectStudent){students_element[CHECKBOX_COL].checkbox_element}
checkbox(:chooseStudent, :id => /student-widget-0/)

def tickStudents(iNumberOfStudents)
    cCurrentRow = 0
    while cCurrentRow < iNumberOfStudents #Check the box for cCurrentRow
        #otherStudents.tableWrapper.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click     #fails
        #allDivs[1].div_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails 
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.checkbox_element.click  #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.check_checkbox_element #fails
        #studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #studentList_element[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
        #students[cCurrentRow][CHECKBOX_COL].checkbox_element.click  #fails
    #   students[cCurrentRow][CHECKBOX_COL].checkbox_element #fails
    #   students[cCurrentRow][CHECKBOX_COL].check_checkbox_element      #fails
    #   students[cCurrentRow][CHECKBOX_COL].check #fails
    #   students[cCurrentRow][CHECKBOX_COL].click #fails
    #   students[cCurrentRow][CHECKBOX_COL].checkbox.checkbox_element.click  #fails
        #students[cCurrentRow].checkbox_element.check  #fails
    #   students[cCurrentRow].element.checkbox.set  #fails
        #students[cCurrentRow][CHECKBOX_COL].checkbox.click     #fails
#studentList_element.table[cCurrentRow][CHECKBOX_COL].checkbox.chooseStudent.click  #fails
        cCurrentRow += 1 #we should have checked the box in current row by now, so move on to next row
    end
end