I am trying to take user input, store it in an array for the number, and then take the dollars and cents portion of the numbers, store those in separate arrays, and then print the formatted number.
This is my code:
<authentication>
<token>6e36dcf1f5d84fcc7062b981891dd2b233bd8373d74f56f03a3bf328560976a2</token>
<anonymous>false</anonymous>
<parameters>
<header>
<userName>ps-subscriber2@mysite.com</userName>
<userType>SUBSCRIPTION_USER</userType>
<Authorization>6e36dcf1f5d84fcc7062b981891dd2b233bd8373d74f56f03a3bf328560976a2</Authorization>
</header>
</parameters>
</authentication>
I am somewhat unsure as to how I could take the cents and store them in an array as a long type. When executing the program, I get the correct dollars but the cents portion always comes out as 0. Any suggestions?
Edit:
So I attempted to solve some of the issues by doing this:
#include <stdio.h>
int main()
{
int i;
float amounts[5];
printf("Enter numbers as dollar amounts: \n");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
for (i=0; i<5; i++) {
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
}
for (i=0; i<5; i++) {
printf("\n$%ld.", dollars[i]);
printf("%ld", cents[i]);
}
return 0;
}
However, now I'm getting everything that is less than 10 for cents[i] to output 1 less than what it should be. So 3.06 prints as $3.05. The same for 3.12 for some reason.
Also, to some of the critique about what variable types I'm using, this is for an assignment and it dictates what types are to be used.
4 Write a program that will read five values from the keyboard (use a loop) and store them in an array of type float with the name amounts. Create two arrays of five elements of type long with the names dollars and cents. Store the whole number part of each value in the amounts array in the corresponding element of dollars and the fractional part of the amount as a two-digit integer in cents (e.g., 2.75 in amounts[1] would result in 2 being stored in dollars[1] and 75 being stored in cents[1]). Output the values from the two arrays of type long as monetary amounts (e.g., $2.75)
答案 0 :(得分:3)
You're losing the fractional part here:
import sys
import serial
import time
import serial.tools.list_ports
from PyQt4 import QtGui
from window_test import Ui_MainWindow
class Main(QtGui.QMainWindow):
def __init__(self):
QtGui.QMainWindow.__init__(self)
self.ser = None
self.ui = Ui_MainWindow()
self.ui.setupUi(self)
self.ui.btn_laser_poweron.clicked.connect(self.btnFIRE)
self.ui.btn_laser_poweroff.clicked.connect(self.btnOFF)
self.ui.btn_lig_power.clicked.connect(self.btnLIG)
self.ui.btn_cutting_power.clicked.connect(self.btnCUT)
self.ui.btn_power_meter.clicked.connect(self.btnTEST)
self.ui.spinBox.valueChanged.connect(self.PwrLevel)
self.ui.comboBox.activated.connect(self.srlprt)
def srlprt(self):
if self.ser:
self.ser.close()
self.ser = serial.Serial(str(self.ui.comboBox.currentText()))
def btnFIRE(self):
self.ser.write("a" + chr(255))
def btnOFF(self):
self.ser.write("b" + chr(0))
def btnTEST(self):
self.ser.write("c" + chr(0))
time.sleep(59.5)
self.ser.write("d" + chr(255))
def btnLIG(self):
self.ser.write("e" + chr(29))
def btnCUT(self):
self.ser.write("f" + chr(160))
def PwrLevel(self):
val = self.ui.spinBox.value()
self.ser.write("g" + chr(val))
if __name__ == '__main__':
app = QtGui.QApplication(sys.argv)
window = Main()
window.show()
sys.exit(app.exec_())
The difference between the two is less than 1, so assigning that value to an cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
results in it getting truncated to 0.
You need to multiply the difference before assigning to avoid truncation:
int
答案 1 :(得分:1)
try: if int(num) ... except ValueError: # num is not able to be converted to int print("You are not giving a number.")
instead use:
for (i=0; i<5; i++) {
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]);
cents[i]*=100;
}
答案 2 :(得分:0)
OP's approach has problems
Test.Form
Negative numbers printf("\n$%ld.", dollars[i]);
printf("%ld", cents[i]);
come out like amount[i] == -1.23
Values like "$-1.-23"
print as amount[i] == 9.998
and likely should be "$9.99"
Missing leading zeros "$10.00"
print as amount[i] == 1.03
@Jonathan Leffler
"$1.3"
fails when dollars[i]=(int)amounts[i]
is outside amounts[i]
range.
A key issue for OP
int
If code is to use FP for money recommend:
Use // problem
dollars[i]=(int)amounts[i];
cents[i]=(amounts[i]-dollars[i]); // cents[i] is an integer, cannot hold a fraction
cents[i]*=100;
// Alternative
// round
amount[i] = round(amount[i] * 100)/100.0;
// take apart;
float ipart;
cents[i] = (int) roundf(modff(amounts[i], &ipart)*100.0f);
dollars[i] = (int) ipart;
.
Round to the nearest monetary unit after any calculation that is not exact. In OP's case this is double
.
1/100
Use a unified print
amount[i] = round(amount[i] * 100)/100.0;
答案 3 :(得分:0)
#include <stdio.h>
#include <math.h>
int main()
{
int i, j;
float amounts[5];
printf("Enter numbers as dollar amounts: \n");
for(i=0; i<5; i++) {
printf("Value %d: ", i+1);
scanf("%f", &amounts[i]);
}
long dollars[5];
long cents[5];
float ipart;
for (i=0; i<5; i++) {
cents[i] = (int) roundf(modff(amounts[i], &ipart)*100.0f);
dollars[i] = (int) ipart;
if(cents[i]==100){
dollars[i]++;
cents[i]=0;
}
}
for (i=0; i<5; i++) {
printf("\n$%ld.", dollars[i]);
if(cents[i]<10)
printf("0%ld", cents[i]);
else
printf("%ld", cents[i]);
}
return 0;
}