I'm developing a simple GTK+3 program that do simple calculation.
I have a problem with floating point number because if i write .
instead of ,
the total will be wrong.
So I have float price;
and i want to replace .
with ,
.
Example: user write 100.60
as price and i want to convert it into 100,60
. How can i do it?
I've thought to strchr
but it is a float and i think i cannot use it xD
Thanks :)
EDIT: added the code
#include <gtk/gtk.h>
#include <stdlib.h>
#include <string.h>
GtkWidget *window;
GtkWidget *grid;
GtkWidget *c_button;
GtkWidget *q_button;
GtkWidget *u_name;
GtkWidget *h_name;
GtkWidget *pass;
GtkWidget *label_user;
GtkWidget *label_host;
GtkWidget *label_pass;
/* Funzione attivata alla pressione del pulsante connect */
int calcolo_guadagno(void){
int num_btp, i, len;
float prezzo_attuale, subtotale, totale, diff_prezzo_attuale, PREZZO_PARTENZA = 99.42;
char cmcd[15];
num_btp = (atoi(gtk_entry_get_text(GTK_ENTRY(h_name)))/PREZZO_PARTENZA);
prezzo_attuale = atof(gtk_entry_get_text(GTK_ENTRY(u_name)));
diff_prezzo_attuale = (prezzo_attuale - PREZZO_PARTENZA);
subtotale = (num_btp * diff_prezzo_attuale);
totale = subtotale-((subtotale/100)*12.5);
if(prezzo_attuale < PREZZO_PARTENZA){
strcpy(cmcd, "Sei in perdita...");
gtk_entry_set_text(GTK_ENTRY(pass),cmcd);
return EXIT_SUCCESS;
}
if(prezzo_attuale == PREZZO_PARTENZA){
strcpy(cmcd, "Sei in pari...");
gtk_entry_set_text(GTK_ENTRY(pass),cmcd);
return EXIT_SUCCESS;
}
sprintf(cmcd, "%.3f", totale);
gtk_entry_set_text(GTK_ENTRY(pass),cmcd);
return EXIT_SUCCESS;
}
int main (int argc, char *argv[]){
/* Inizializziamo le librerie GTK+ */
gtk_init (&argc, &argv);
/* Creiamo la finestra principale, impostiamo il titolo, la posizione e la dimensione */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Calcolo Guadagno BTP");
gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size (GTK_WINDOW(window), 350, 250);
/* Quando premiamo chiudi si 'disturgge' tutto */
g_signal_connect (window, "destroy", G_CALLBACK (gtk_main_quit), NULL);
/* Impostiamo il bordo e dichiariamo le finestra ridimensionabile */
gtk_container_set_border_width (GTK_CONTAINER (window), 10);
gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
/* Creiamo una nuova griglia, la aggiungiamo alla finestra e impostiamo gli spazi fra i vari widget */
grid = gtk_grid_new ();
gtk_container_add (GTK_CONTAINER (window), grid);
gtk_grid_set_row_spacing (GTK_GRID (grid), 3);
/* Creiamo delle label con il relativo nome */
label_user = gtk_label_new("Prezzo attuale ");
label_host = gtk_label_new("Soldi investiti ");
label_pass = gtk_label_new("Guadagno netto ");
/* Creo una entry_text */
h_name = gtk_entry_new();
/* Impostiamo il testo trasparente che scompare quando clicco o scrivo */
gtk_entry_set_placeholder_text (GTK_ENTRY (h_name), "Soldi investiti");
u_name = gtk_entry_new();
gtk_entry_set_placeholder_text (GTK_ENTRY (u_name), "Prezzo attuale");
pass = gtk_entry_new();
/* Impostiamo il campo password non visibile */
gtk_editable_set_editable (GTK_EDITABLE (pass), 0);
/* Creiamo i pulsanti e li connettiamo alle relative funzioni */
c_button = gtk_button_new_with_label ("Calcola");
g_signal_connect (c_button, "clicked", G_CALLBACK (calcolo_guadagno), NULL);
q_button = gtk_button_new_with_label ("Esci");
g_signal_connect (q_button, "clicked", G_CALLBACK (gtk_main_quit), NULL);
/* Imposto i widget come espandibili sia in orizzontale che in verticale */
gtk_widget_set_hexpand(u_name, TRUE);
gtk_widget_set_vexpand(u_name, TRUE);
gtk_widget_set_hexpand(h_name, TRUE);
gtk_widget_set_vexpand(h_name, TRUE);
gtk_widget_set_hexpand(pass, TRUE);
gtk_widget_set_vexpand(pass, TRUE);
/* Posizioniamo i widget all'interno della griglia */
gtk_grid_attach (GTK_GRID (grid), label_host, 0, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), h_name, 1, 0, 1, 1);
gtk_grid_attach (GTK_GRID (grid), label_user, 0, 1, 1, 1);
gtk_grid_attach (GTK_GRID (grid), u_name, 1, 1, 2, 1);
gtk_grid_attach (GTK_GRID (grid), label_pass, 0, 2, 1, 1);
gtk_grid_attach (GTK_GRID (grid), pass, 1, 2, 1, 1);
gtk_grid_attach (GTK_GRID (grid), c_button, 0, 3, 2, 1);
gtk_grid_attach (GTK_GRID (grid), q_button, 0, 4, 2, 1);
/* Mostriamo tutti i widget */
gtk_widget_show_all (window);
/* Entriamo nel loop principale delle GTK+, da questo punto l'applicazione attende l'accadere di un qualsiasi evento */
gtk_main ();
return 0;
}
Having prices as floats is in general a very bad idea. floating-point arithmetic is imprecise and will come back to bite you in very weird situations. Use a struct
to represent both parts of the currency and then implement some arithmetic and printing with them.
type struct {
int main;
int sub;
} money;
void add(money* result, money* x, money* y);
void print(money* x);
void read(money* result, const char* input);
Your print function can explicitly specify what separator should be used.
Have a look at setlocale .
If you are using a locale that uses comma as seperator, it will be used in input/output operations.
Is that what you are looking for?
The strpbrk function in <string.h>
can help.
void dot_to_comma(char *input) {
char *ptr = NULL;
while(ptr = strpbrk(input, ".")) { //find the first dot in input
*ptr = ','; //replace the dot with a comma
}
}
int i;
int len = strlen(string);
for (i = 0; i < len; i++) {
if (string[i] == '.') {
string[i] = ',';
i = len; // or `break;`
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.