#!/usr/bin/env python
#-*- coding: iso-8859-1 -*-
import sys
def eur(a):
""" Changing float to EUR-string. """
b = "%.2f" % a
b = b.replace('.',',')
c = b.split(",")
return thpoints(c[0]) + "," + c[1]
def thpoints(a):
""" Returns a string with points for thousands in high values ...
... from a positive or negative int. """
a = int(a)
neg = False
if a < 0:
neg = True
a = a * -1
a = str(a)
b = []
for i in a:
b.append(i)
x = len(b)
while x > 3:
x -= 3
b.insert(x, ".")
c = "".join(b)
if neg:
c = "-" + c
return c
if len(sys.argv) > 1:
print eur(float(sys.argv[1]))
./skript.py 1222111.55
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/* eur.c */
char *eur(double v);
char *thpoints(char *a);
int main(int argc, char **argv)
{
if(argc > 1)
{
puts(eur(atof(argv[1])));
}
return 0;
}
char *eur(double v)
{
char *a;
char *s;
s = (char *) malloc(50);
sprintf(s, "%.2f", v);
a = strstr(s, ".");
if(a != NULL)
{
*a = ',';
}
s = thpoints(s);
return s;
}
char *thpoints(char *a)
{
char b[50];
char *c = (char *) malloc(50);
char *d = strstr(a, ",");
int i;
int x = 0;
int e = 0;
for(i = strlen(a) - 1; i >= 0; i--)
{
if(*(a + i) == ',')
{
x = 1;
}
if(x == 1 && *(a + i) != ',')
{
if((double) (strlen(a) - i - 1) / 3 == (strlen(a) - i - 1) / 3)
{
b[e] = '.';
if(e != 0)
{
e++;
}
}
b[e] = *(a + i);
e++;
}
}
b[e] = '\0';
for(i = strlen(b) - 1; i >= 0; i--)
{
c[strlen(b) - 1 - i] = b[i];
}
strcat(c, d);
free(a);
return c;
}
gcc -g -Wall -Wextra -ansi -pedantic eur.c -o eurstring
Das kommt auf die Sprache (und leider auch auf die Plattform) an.lars22 schrieb:ich suche nach einer Möglichkeit eine Zahl zu formatieren.
Kann man mit dem printf Befehl eine Zahl von 1222111.55 nach 1.222.111,55 formatieren?
#include <stdio.h>
#include <locale.h>
int main (int argc, char **argv)
{
char *locale;
locale = setlocale(LC_NUMERIC, "de_DE.UTF-8");
printf("Locale: %s\n%'.2f\n", locale, 1222111.55);
locale = setlocale(LC_NUMERIC, "en_US.UTF-8");
printf("Locale: %s\n%'.2f\n", locale, 1222111.55);
return(0);
}
Locale: de_DE.UTF-8
1.222.111,55
Locale: en_US.UTF-8
1,222,111.55
Das ist wirklich ganz schlechter Stil und Wasser auf die Mühlen der C Verächter.abgdf schrieb:(Ja, solange mich der Compiler nicht stoppt, caste ich malloc(); stellt euch auf den Kopf :mrgreen:. Aber, wenn's Speicherlecks gibt, bitte melden.)
For some numeric conversions a radix character (`decimal point') or
thousands' grouping character is used. The actual character used
depends on the LC_NUMERIC part of the locale. The POSIX locale uses
`.' as radix character, and does not have a grouping character. Thus,
printf("%'.2f", 1234567.89);
results in `1234567.89' in the POSIX locale, in `1234567,89' in the
nl_NL locale, and in `1.234.567,89' in the da_DK locale.
Deshalb habe ich ja auch darauf hingewiesen dass das von der Sprache abhängt.abgdf schrieb:Seltsam aber: Python und Perl scheinen mit "%'.2f" nichts anfangen zu können ...
... damit müsste es doch auch das bash-builtin können.jengelh schrieb:Das geht auch in C, man muss nur mal die Augen aufsperren. `man 3 printf`:
prompt> locale | grep NUMERIC
LC_NUMERIC="POSIX"
prompt> printf "%'.2f" 1234567.899
1234567.90
prompt> export LC_NUMERIC=de_DE.UTF-8
prompt> printf "%'.2f" 1234567.899
bash: printf: 1234567.899: invalid number
0,00
prompt> printf "%'.2f" 1234567,899
1.234.567,90
prompt>
$ LC_NUMERIC=de_DE.UTF-8 printf "%'.2f\n" 12345678
12.345.678,00
Ja moment. Von der Eingabe war nie die Rede; die muss weiterhin in POSIX vorliegen. Ausgabe klappt doch mit Thousands-Separator und Komma, also alles ok. Kein Problem in Sicht.prompt> printf "%'.2f" 1234567.899
bash: printf: 1234567.899: invalid number
0,00
prompt> printf "%'.2f" 1234567,899
1.234.567,90
#!/usr/bin/env python
import locale
locale.setlocale(locale.LC_NUMERIC, 'de_DE.UTF-8')
print locale.format("%.2f", 1222111.55, 1)
import java.lang.Object;
import java.text.Format;
import java.text.NumberFormat;
public class format {
public static void main(String[] args) {
String blub = "";
for (int i=0;i<args.length;i++) {
blub += args[i];
}
try{
double d = Double.valueOf(blub);
String myString = NumberFormat.getInstance().format(d);
System.out.println(myString);
} catch (NumberFormatException e) {}
}
}