从美分到美元的数字格式

问题描述 投票:0回答:5

我正在尝试使用 Python 2.7 的字符串格式以美元输出 10 个苹果的成本,其中单价以美分为单位。

我希望

total_apple_cost
的值为
"10.00"
,但它是
"1.001.001.001.001.001.001.001.001.001.00"

我已经对其他变量进行了测试,以表明它们都按预期出现:

# define apple cost in cents
apple_cost_in_cents = 100
# define format string
cents_to_dollars_format_string = '{:,.2f}'
# convert 100 to 1.00
apple_cost_in_dollars = cents_to_dollars_format_string.format(apple_cost_in_cents / 100.)
# assign value of 'apple_cost_in_dollars' to 'apple_cost'
apple_cost = apple_cost_in_dollars
# calculate the total apple cost
total_apple_cost = 10 * apple_cost

# print out the total cost
print 'total apple cost: ' + str(total_apple_cost) + '\n'

#testing
print 'cost in cents: ' + str(apple_cost_in_cents) + '\n'
print 'cost in dollars: ' + str(apple_cost_in_dollars) + '\n'
print 'apple cost: ' + str(apple_cost) + '\n' 

解决方案:

感谢下面的答案,它们都表明变量“apple_cost_in_dollars”是一个字符串。

我的解决方案是使它成为一个浮动并保持代码的其余部分几乎相同:

apple_cost_in_cents = 100
cents_to_dollars_format_string = '{:,.2f}'
apple_cost_in_dollars = float(cents_to_dollars_format_string.format(apple_cost_in_cents / 100.))
apple_cost = apple_cost_in_dollars
total_apple_cost = 10 * apple_cost

print 'cost in cents: ' + str(apple_cost_in_cents) + '\n'

print 'cost in dollars: $''{:,.2f}'.format(apple_cost_in_dollars) + '\n'

print 'apple cost: $''{:,.2f}'.format(apple_cost) + '\n'

print 'total apple cost: $''{:,.2f}'.format(total_apple_cost) + '\n'
python python-2.7 number-formatting
5个回答
4
投票

这是因为

apple_cost_in_dollars
是一个字符串,见下文

In [9]: cost = '1'

In [10]: cost * 10
Out[10]: '1111111111'

In [11]: cost = int('1')

In [12]: cost * 10
Out[12]: 10

2
投票

apple_cost
是一个字符串,您将其乘以 10(这只是将字符串重复 10 次)。在将其格式化为字符串之前,先将其转换为美元。

>>> apple_cost_in_cents = 100
>>> cents_to_dollars_format_string = '{:,.2f}'
>>> total_apple_cost_in_dollars_as_string = cents_to_dollars_format_string.format(10*apple_cost_in_cents/100.0)
>>> total_apple_cost_in_dollars_as_string
'10.00'

如果您想进一步设置货币格式,您可以查看 locale 模块,特别是

locale.currency
函数。


2
投票
>>> import locale
>>> apple_cost_in_cents = 100
>>> locale.setlocale(locale.LC_ALL, '')
'en_US.UTF-8'
>>> locale.currency(apple_cost_in_cents * 10 / 100)
'$10.00'

1
投票

它被格式化为字符串(文本)。所以如果你写 10 * string_variable,它只是重复该字符串 10 次。最简单的方法是更改此行:

total_apple_cost = 10 * apple_cost

至:

total_apple_cost = cents_to_dollars_format_string.format(10 * apple_cost_in_cents/100)


0
投票

导入java.util.ArrayList;

公共类股票价格分析{

// Method to calculate the average stock price
public static double calculateAveragePrice(int[] stockPrices) {
    double sum = 0;
    for (int price : stockPrices) {
        sum += price;
    }
    return sum / stockPrices.length;
}

// Method to find the maximum stock price
public static int findMaximumPrice(int[] stockPrices) {
    int maxPrice = stockPrices[0];
    for (int price : stockPrices) {
        if (price > maxPrice) {
            maxPrice = price;
        }
    }
    return maxPrice;
}

// Method to determine the occurrence count of a specific price
public static int countOccurrences(int[] stockPrices, int targetPrice) {
    int count = 0;
    for (int price : stockPrices) {
        if (price == targetPrice) {
            count++;
        }
    }
    return count;
}

// Method to compute the cumulative sum of stock prices
public static ArrayList<Integer> computeCumulativeSum(ArrayList<Integer> stockPrices) {
    ArrayList<Integer> cumulativeSum = new ArrayList<>();
    int sum = 0;
    for (int price : stockPrices) {
        sum += price;
        cumulativeSum.add(sum);
    }
    return cumulativeSum;
}

public static void main(String[] args) {
    // Example data
    int[] stockPricesArray = {10, 20, 30, 40, 50, 60, 70, 80, 90, 100};
    ArrayList<Integer> stockPricesList = new ArrayList<>();
    for (int price : stockPricesArray) {
        stockPricesList.add(price);
    }

    // Calculating the average price
    double averagePrice = calculateAveragePrice(stockPricesArray);
    System.out.println("Average Stock Price: " + averagePrice);

    // Finding the maximum price
    int maxPrice = findMaximumPrice(stockPricesArray);
    System.out.println("Maximum Stock Price: " + maxPrice);

    // Counting occurrences of a specific price
    int targetPrice = 50;
    int occurrences = countOccurrences(stockPricesArray, targetPrice);
    System.out.println("Occurrences of price " + targetPrice + ": " + occurrences);

    // Computing the cumulative sum
    ArrayList<Integer> cumulativeSum = computeCumulativeSum(stockPricesList);
    System.out.println("Cumulative Sum of Stock Prices: " + cumulativeSum);
}

}

© www.soinside.com 2019 - 2024. All rights reserved.