Author Topic: question by a total beginner print number with two number after comma  (Read 4602 times)

0 Members and 1 Guest are viewing this topic.

Offline Christo92

  • Newbie
  • Posts: 2
    • View Profile
question by a total beginner
i'm trying to learn QB64
my question can being stupid
but i would like print  nombre  with two number after comma
thank for your help

10 PRINT " TOP JS   par christophe lamadon"
20 INPUT " BILAN "; a
30 INPUT " COTE GAGNANTE "; b
40 INPUT "COTE PLACE"; c
50 CGP = b + c
60 M = b / CGP
70 N = (b - 1)
80 Z = a * N
90 E = M / Z
100 nombre = E

Offline Juanjogomez

  • Forum Regular
  • Posts: 117
    • View Profile
Re: question by a total beginner print number with two number after comma
« Reply #1 on: August 02, 2018, 01:23:09 pm »
You can print with then command:

PRINT USING "#,###,###.##";nombre


Offline SMcNeill

  • QB64 Developer
  • Forum Resident
  • Posts: 3972
    • View Profile
    • Steve’s QB64 Archive Forum
Re: question by a total beginner print number with two number after comma
« Reply #2 on: August 02, 2018, 02:35:05 pm »
PRINT INT(nombre * 100 ) / 100
https://github.com/SteveMcNeill/Steve64 — A github collection of all things Steve!

Offline Christo92

  • Newbie
  • Posts: 2
    • View Profile
Re: question by a total beginner print number with two number after comma
« Reply #3 on: August 03, 2018, 05:08:15 pm »
Thank you very much for your help


Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: question by a total beginner print number with two number after comma
« Reply #4 on: August 14, 2018, 01:04:59 pm »
Hi Juanjogomez,

You can print with then command:
PRINT USING "#,###,###.##";nombre

The problem with the command PRINT USING "mask";number is:
a) this prints spaces in front of the integer part of the number if the value is smaller than the mask,
b) and prints the sign % in front of the integer part of the number if its value is larger than the mask.

I turned arround this problem writing a small but inelegant small program that formats a float without the leading spaces.

The code is below:

Code: QB64: [Select]
  1. '
  2. ' program name: Print_Using.bas
  3. '
  4. ' This small program formats a float with two separate blocks.
  5. ' The first block is the interger part of the float separated
  6. ' in groups of three didits by a comma, the second group is
  7. ' the decimal part of the float rounded to 2 digits after the
  8. ' decimal point.
  9. ' The main purpose of this program is to format a float without
  10. ' using the PRINT USING command with a mask that places spaces
  11. ' in front of the number when the float is smaller than the mask
  12. ' and that puts the % character in front of the number when the float
  13. ' is larger than the mask.
  14. '
  15. '
  16. ' title of the windows when used in a console
  17. '
  18. _TITLE "Print_Using"
  19. '
  20. ' variable initialisation
  21. '
  22. Number# = 1.00
  23. '
  24. ' main program
  25. '
  26. WHILE Number# <> 0
  27.     PRINT "Please enter a float: ";
  28.     INPUT (Number#)
  29.     PRINT "Using this function will print:" + Print_Using$(Number#)
  30.     PRINT "PRINT USING command will print:";
  31.     PRINT USING "#,###,###,###.##"; Number#
  32. '
  33. ' function to format the integer part of a float and round its decimal part at 2 digits.
  34. '
  35. FUNCTION Print_Using$ (nb#)
  36.     Print_Using$ = intPart(nb#) + decPart(nb#)
  37. '
  38. ' function to format and round the decimal part of a float at 2 digits.
  39. '
  40. FUNCTION decPart$ (nb#)
  41.     dot$ = "."
  42.     dotPos% = INSTR(LTRIM$(STR$(nb#)), dot$)
  43.     IF dotPos% = 0 THEN
  44.         partdec$ = ".00"
  45.     ELSE
  46.         partdec$ = MID$(LTRIM$(STR$(nb#)), dotPos%)
  47.         strlen% = LEN(partdec$)
  48.     END IF
  49.     IF strlen% = 1 THEN
  50.         partdec$ = ".00"
  51.     ELSE
  52.         SELECT CASE strlen%
  53.             CASE 2
  54.                 partdec$ = partdec$ + "0"
  55.             CASE IS > 3
  56.                 IF VAL(MID$(partdec$, 4, 1)) >= 5 THEN
  57.                     MID$(partdec$, 3, 1) = LTRIM$(STR$(VAL(MID$(partdec$, 3, 1)) + 1))
  58.                 END IF
  59.                 partdec$ = LEFT$(partdec$, 3)
  60.         END SELECT
  61.     END IF
  62.     decPart$ = partdec$
  63. '
  64. ' function to format the integer part of a float with groups of 3 digits.
  65. '
  66. FUNCTION intPart$ (nb#)
  67.     i$ = LTRIM$(RTRIM$(STR$(FIX(nb#))))
  68.     IF nb# < 0 THEN
  69.         i$ = MID$(i$, 2)
  70.     END IF
  71.     lenI = LEN(i$)
  72.     IF lenI > 3 THEN
  73.         m3 = lenI MOD 3
  74.         IF m3 <> 0 THEN
  75.             i$ = LEFT$(i$, m3) + "," + MID$(i$, m3 + 1)
  76.         END IF
  77.         idx = m3 + 4 + (m3 = 0)
  78.         WHILE idx < LEN(i$)
  79.             i$ = LEFT$(i$, idx) + "," + MID$(i$, idx + 1)
  80.             idx = idx + 4
  81.         WEND
  82.     ELSEIF lenI = 0 THEN
  83.         i$ = "0"
  84.     END IF
  85.     IF nb# < 0 AND lenI > 0 THEN
  86.         i$ = "-" + i$
  87.     END IF
  88.     intPart$ = i$
  89.  

A better solution would be:

1) to modifiy the PRINT USING command in the way ANSI C or PHP printf() commands behave i.e.: printf("%.2f", float).

2) or to use the standard stdio.h library to be able to use the printf() and sprintf() and other commands of the standard C library.

However, I don't know how to use such an external C library with QB64.

Any idea?

Best regards.

It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.

Offline Fifi

  • Forum Regular
  • Posts: 181
    • View Profile
    • My small QB64 contribution
Re: question by a total beginner print number with two number after comma
« Reply #5 on: August 14, 2018, 04:24:49 pm »
Hi All,

I'd appreciate your help to customise the small Print_Using.bas sample provided in the post above to be able to pass to the main function Print_Using(nb#) two new parameters that would be used by the two other functions IntPart(nb#) and DecPart(nb#) :

1) the character to be used as the decimal separator of the decimal part of a float (that could be a point "." or a comma ",")

2) the character to be used as the interger group separator (that could be empty if you don't want a group separator).

The goal is to use something like this:

nb# = 12345.678
IntSeparator$ = ","
DecSeparator$ = "."

Print_Using(nb#, IntSeparator$, DecSeparator$)

DecPart(nb#, DecSeparator$)

InnPart(nb#, IntSeparator$)

I've tried different solutions but I can't get the result I'm expecting since I'm not QB64 fluent enough.

Any help would be appreciated.

Best regards.
It's better to look like an idiot for a short time while asking something obvious to an expert than pretending to be smart all your life. (C) Me.