How to format a number 1000 as “1 000”

前端 未结 12 1028
北海茫月
北海茫月 2020-12-14 06:19

I need a way to format numbers. I stored some numbers in my DB table, e.g. 12500, and would like to print them in this format 12 500 (so there is a

相关标签:
12条回答
  • 2020-12-14 07:00

    see: http://www.justskins.com/forums/format-number-with-comma-37369.html

    there is no built in way to it ( unless you using Rails, ActiveSupport Does have methods to do this) but you can use a Regex like

    formatted_n = n.to_s.reverse.gsub(/...(?=.)/,'\&,').reverse
    
    0 讨论(0)
  • 2020-12-14 07:01

    So, this is pretty crazy and hackish, but it gets the job done...

    12500.to_s.split("").reverse.each_slice(3).map {|y| y.join("").reverse}.reverse.join(" ")
     => "12 500" 
    
    .to_s: convert to string
    .split(""): split into separate digits
    .reverse: reverse order
    .each_slice(3): peel of each three digits (working from back end due to reverse)
    .map {|y| y.join("").reverse}: map into an array for each three digits - join back together with no delimiter and reverse order back to original
    .reverse: reverse order of mapped array
    .join(" "): join mapped array back together with space delimiter
    
    0 讨论(0)
  • 2020-12-14 07:02

    Here's another method that is fairly clean and straightforward if you are dealing with integers:

    number.to_s.reverse.scan(/\d{1,3}/).join(",").reverse
    
    number            #=> 12345678
    .to_s             #=> "12345678"
    .reverse          #=> "87654321"
    .scan(/\d{1,3}/)  #=> ["876","543","21"]
    .join(",")        #=> "876,543,21"
    .reverse          #=> "12,345,678"
    

    Works great for integers. Of course, this particular example will separate the number by commas, but switching to spaces or any other separator is as simple as replacing the parameter in the join method.

    0 讨论(0)
  • 2020-12-14 07:03

    Another way: Here "delimiter is ' '(space), you can specify ',' for money conversion."

    number.to_s.reverse.gsub(%r{([0-9]{3}(?=([0-9])))}, "\\1#{delimiter}").reverse
    
    0 讨论(0)
  • 2020-12-14 07:03

    I'm aware this is an old question but.

    why not just use a substring substitution.

    in pseudo code....

    String numberAsString = convertNumberToString(123456);
    int numLength = V.length;//determine length of string
    
    String separatedWithSpaces = null;
    
    for(int i=1; i<=numlength; i++){//loop over the number
    separatedWithSpaces += numberAsString.getCharacterAtPosition(i);
        if(i.mod(3)){//test to see if i when devided by 3 in an integer modulo,
        separatedWithSpaces += " ";
    
        }//end if
    
    }//end loop
    

    I know it isn't in any particular languange, but hopefully you get the idea.

    David

    0 讨论(0)
  • 2020-12-14 07:08

    The official document suggests three different ways:

    1) Using lookbehind and lookahead (Requires oniguruma)

    12500.to_s.gsub(/(?<=\d)(?=(?:\d{3})+\z)/, ' ')
    # => "12 500"
    

    2) Using only lookahead. Identical to steenslag's answer.

    3) Using neither lookahead nor lookbehind

    s = 12500.to_s
    nil while s.sub!(/(.*\d)(\d{3})/, '\1 \2')
    s # => "12 500"
    
    0 讨论(0)
提交回复
热议问题