带对齐的字符串格式货币

时间:2017-03-07 21:34:27

标签: python python-3.x

<script src="https://code.jquery.com/jquery-3.1.1.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<script>
    $(document).ready(function(){
        var progress = setInterval(function() {
        var $bar = $('.progress-bar');

        if ($bar.width()>=400) {
            clearInterval(progress);
            $('.progress-bar').removeClass('active');
        } else {
            $bar.width($bar.width()+40);
        }
        //$bar.text($bar.width()/4 + "%");
    }, 1);
    });
</script>
<style>
    .progress .progress-bar {
        -moz-animation-name: animateBar;
        -moz-animation-iteration-count: 1;
        -moz-animation-timing-function: ease-in;
        -moz-animation-duration: .4s;

        -webkit-animation-name: animateBar;
        -webkit-animation-iteration-count: 1;
        -webkit-animation-timing-function: ease-in;
        -webkit-animation-duration: .4s;

        animation-name: animateBar;
        animation-iteration-count: 1;
        animation-timing-function: ease-in;
        animation-duration: .4s;
    }

    @-moz-keyframes animateBar {
        0% {-moz-transform: translateX(-100%);}
        100% {-moz-transform: translateX(0);}
    }
    @-webkit-keyframes animateBar {
        0% {-webkit-transform: translateX(-100%);}
        100% {-webkit-transform: translateX(0);}
    }
    @keyframes animateBar {
        0% {transform: translateX(-100%);}
        100% {transform: translateX(0);}
    }
</style>

打印出来:

print('this is £{0:>10.2f}'.format(3.44))

即。将this is £ 3.44 与金额分开。有没有办法在正确的位置打印带有货币符号的打印间隔列,在金额旁边? E.g。

£

我尝试了this is £3.44 ,但得到了:

print('this is {0:£>10.2f}'.format(3.44))

2 个答案:

答案 0 :(得分:2)

您需要分两步执行此操作,一个用于货币符号和小数位,另一个用于右对齐空格:

>>> 'this is {:>10}'.format('£{:.2f}'.format(3.44))
'this is      £3.44'

如果您想要逗号分隔数千,您还可以在格式规范中包含,

>>> 'this is {:>10}'.format('£{:,.2f}'.format(1003.44))
'this is  £1,003.44'

答案 1 :(得分:-1)

你使用右对齐{:&gt; 10},如果你想左对齐,试试这个:

In[21]: print('this is £{0:<10.2f}'.format(3.44))
this is £3.44