As of May 31, 2023, we have updated our Code of Conduct.

Questions tagged [formatting]

For programs that are concerned with producing meaningful or clearer output. A typical example would be the transformation of a decimal into a particular currency format with the correct number of decimal places. (Note that code layout is always in scope for answers, so this tag is not used to indicate perceived issues with that).

Filter by
Sorted by
Tagged with
0 votes
1 answer
46 views

code to format text input to 2 decimal places before computation

I have this piece of code that checks the input of a text box before I compute for my main logic. This looks like it should be refactored but I'm currently stumped how to do so. The only thing that ...
jumping_bear's user avatar
2 votes
2 answers
149 views

Calculate the price of fruits in a trade (for a game)

I'm making a calculator that is supposed to calculate the price of fruits in a trade (for a game) omg, my code is so bad and messy that its embarrassing. I'm so sorry for anyone who has to look at ...
Lhinger's user avatar
  • 31
1 vote
2 answers
83 views

Optimizing - formatting a person's name

I need to take a user's name and format it 'lastName, firstName middleName'. If firstName and middleName are not present then the comma should not be included with lastName. Below is what I came up ...
Samuel's user avatar
  • 21
2 votes
1 answer
94 views

Clean up improperly formatted phone numbers

This is a problem from Exercism here, the goal is to clean up badly formatted phone numbers. The rules are: If the phone number is less than 10 digits assume that it is a bad number If the phone ...
Josh Friedlander's user avatar
0 votes
1 answer
107 views

Short Text Pre-processing

For educational purpose I am preprocessing multiple short texts containing the description of the symptoms of cars fault. The text is written by humans and is rich in misspelling, capital letters and ...
Andrea Ciufo's user avatar
4 votes
2 answers
692 views

Logger that writes to text file with std::vformat

This class has a Log function that appends text to a log text file. It takes a format string and a variable amount of arguments, much like a ...
vince.smdt's user avatar
2 votes
1 answer
95 views

Multithreaded segmented Sieve of Eratosthenes

I am fairly new to Rust and thought a good way to practice would be to write a multithreaded segmented Sieve of Eratosthenes. It performs ok (searches ten-billion numbers in about 11 seconds on my ...
knots427's user avatar
2 votes
1 answer
83 views

Build a message string depending on the type of signature of an invoice

The following method was meant to build a message depending on the type of signature of an invoice. It contains some if-else statements and code duplication. The original method : ...
l0r3nz4cc10's user avatar
0 votes
0 answers
210 views

Angular Kendo date formatting directive

I created the following directive to format dates in kendo grid cells, in Angular: ...
Jason's user avatar
  • 385
3 votes
2 answers
161 views

K&R Exercise 5-11. Modify the "detab" - to accept a list of tab stops as arguments

So far I have reached chapter 5 of the K&R Book, Edition 2 from which I have been learning C. I spent a few days thinking of a solution to this program. I wrote two or three versions of the same ...
jr.çhåvez__07's user avatar
2 votes
1 answer
121 views

K&R Exercise 1.22 - "fold" long input lines

Until recently, I decided to go back to Chapter 1 of the K&R Book, Edition 2, to try to "improve" the code I've already done. I have also made some changes since I was limited to just ...
jr.çhåvez__07's user avatar
3 votes
2 answers
163 views

K&R Exercise 4-6. Add commands for handling variables

For the following exercise (Chapter 4, Ex-4.6) of the Book (K&R 2nd Edition): Exercise 4-6. Add commands for handling variables. (It's easy to provide twenty-six variables with single-letter ...
jr.çhåvez__07's user avatar
8 votes
3 answers
772 views

K&R Exercise 3-3. Expands shorthand notations (e.g., a-z to abc..xyz, 0-9 to 012..789)

I have been learning C with K&R Book 2nd Ed. So far I have completed quite a few exercises. For the following exercise (Chapter 3, Ex-3.3): Exercise 3-3. Write a function expand(s1, s2) that ...
jr.çhåvez__07's user avatar
7 votes
3 answers
1k views

K&R Exercise 1-19. "detab" program that replaces tabs with the appropriate number of blanks

I am trying to do a few exercises from the K&R book from the first chapter - exercise 1-19. I have to write a program "detab" which replaces tabs in the input with the proper number of ...
jr.çhåvez__07's user avatar
3 votes
2 answers
3k views

Printing a C++ struct

Critique? ...
user1095108's user avatar
  • 1,404
5 votes
1 answer
604 views

IFormatProvider for double to engineering notation

A c# format provider that formats a double into a string using engineering notation rules. If the value is outside of the defined notation symbols it will default ...
Nifim's user avatar
  • 197
5 votes
2 answers
2k views

C++ I wrote a simple and fast formatting library for strings

C++ FString A quick C++ formatting library I was working on a logging library, and needed some way to format strings quickly, simple and clean, so I wrote this library. This library is not the best in ...
Maysara Elshewehy's user avatar
3 votes
2 answers
84 views

Re-designing c-style Input Output

Re - designing c-style input output [ btw I use printf() and scanf() in the core ] Reasons for redesigning it :- Needs 2 functions for input output [ namely ...
Pear's user avatar
  • 243
0 votes
2 answers
383 views

12 hour to 24 hour time conversion

doing hackerrank challenges and been learning python3 for a month now, made this code to convert and return 12 hour to 24 hour time format in one of the challenges ...
Physcofury's user avatar
3 votes
1 answer
320 views

LeetCode 68. Text Justification

I am trying out Leetcode problem 68 and I need your help to asses the code. I am pretty new to this. Given an array of strings words and a width ...
Chintamani Chavan's user avatar
1 vote
1 answer
35 views

Rust program to print citations in Chicago author-date style

I have written a Rust program that lets you input a citation's metadata in a Bibtex-like format and generates a bibliography entry formatted in Markdown that conforms (more or less) to the Chicago ...
Max's user avatar
  • 207
0 votes
1 answer
668 views

Integer array to string

This function creates takes an int * buffer and creates a neatly formatted string (useful for printing the contents of an array). Is the code easy to follow? Is it ...
cgoates's user avatar
  • 155
4 votes
1 answer
165 views

Parse floats to a custom numeric format string

I'm working to implement a custom string format. These are the float values to convert into a string: ...
jms2505's user avatar
  • 43
3 votes
1 answer
657 views

Python function to output a string that is justified to the right

I just solved this exercise from Think Python, 2nd edition: Python provides a built-in function called len that returns the length of a string, so the value of len('allen') is 5. Write a function ...
pedromartins's user avatar
0 votes
1 answer
64 views

custom button class for android game engine

I'm a self-taught programmer and I haven't really followed industry-standard coding classes. I'm currently coding my own game engine for android, though and I was wondering if my coding style could ...
Gyoo's user avatar
  • 3
8 votes
1 answer
114 views

Converting a number to a string with a maximum of 3 numerals plus SI unit

I wrote a function that receives a number as an argument and returns a number (technically, a string) with a maximum of 3 numerals, no matter if there's a decimal separator or not, plus the adequate ...
Megaptera novaeangliae's user avatar
2 votes
1 answer
146 views

Encode non-negative integer to a string of ascii letters and digits

I want to encode a non-negative integer to ASCII letters and digits. I wrote below code for that, that works, but which I assume too immature to use in production. ...
Richard Neumann's user avatar
2 votes
4 answers
592 views

Formatted output of a phone number from an array of int

I wrote a function that takes an array of 10 integers (from 0 to 9) that returns a string of those numbers as a phone number. Example: ...
Konstantine's user avatar
3 votes
1 answer
1k views

Format an integer with space as thousand separator

I need to format a number with space as thousand separator in my Java code for Android. How can these functions be improved? What are the pros and cons to the first recursive method? What are the ...
Ola Ström's user avatar
19 votes
4 answers
3k views

Minimal `printf` for integer types in x86 assembly

I'm writing a minimal C runtime targeting an old 32-bit Windows XP machine as a personal project. The C runtime provided by compilers is quite bloated. I wouldn't mind some library bloats up to ...
xiver77's user avatar
  • 421
1 vote
2 answers
765 views

Create an ASCII table from CSV

The following code convert csv to ascii table. ...
Ahmad Ismail's user avatar
4 votes
0 answers
38 views

formatting flattened string into JSON-like object in C++, part 2

I have reworked the code from my previous (linked) post. For reference: I have a string which contains JSON-like object flattened (stringify()ed). I need to JSON-...
user3132457's user avatar
2 votes
1 answer
99 views

formatting flattened string into JSON-like object in C++

I have a string which contains JSON-like object flattened (stringify()ed). I need to JSON-ify it back. It's a custom format, so I wrote function to JSON-ify the ...
user3132457's user avatar
-2 votes
2 answers
127 views

Write conditional expression better [closed]

I have the following code function x () { return a ? (b ? 'result2' : 'result1') : 'result1'; } Is there a better way to combine these two conditions into one?...
AngularDebutant's user avatar
3 votes
2 answers
489 views

Simple string manipulation in C (for small microcontrollers)

Although I have long experience of other languages, I've not used C until playing around with microcontrollers using the Arduino IDE. I'm a bit uncomfortable with my use of pointers in conjunction ...
RedGrittyBrick's user avatar
0 votes
2 answers
99 views

Is there a neater, more elegant way to format this output?

I have this code that writes the initial configuration of a simulator I'm writing to stdout. args is an instance of type ...
SRSR333's user avatar
  • 103
2 votes
1 answer
85 views

Beginner Q: Is there a cleaner way to group a list in JS?

I'm looking to highlight some words on a webpage, I've grouped them now, but changed the css and html slightly and this seems to have broken it. For context, looking to add a list of words in JS which ...
chipsndip's user avatar
4 votes
1 answer
386 views

Gear Shift Indicator using Hall Effect Sensors & 8x8 LED Display

I wired up a Gear Shift Indicator for my dad's car to display what gear it is currently in, and showing changes with animations (sliding up and down as appropriate, with a few bonus ones for fun at ...
Phrontistes's user avatar
1 vote
2 answers
61 views

Colour message prettifier in Bash

Features: messages formatted with clear borders errors sent to stderr colour handling colours can be included in other strings echoed to the screen without having ...
CJ Dennis's user avatar
  • 239
-1 votes
2 answers
632 views

DiscountManager class in C#

I built a class in C#, DiscountManager, which is responsible for calculating a customer discount based on years of loyalty. I want to refactor it and am seeking any ...
MD. RAKIB HASAN's user avatar
3 votes
0 answers
257 views

Use formats phone numbers into XXX-XXX-XXX

I created an input that accepts and formats phone numbers into XXX-XXX-XXX. The user should be able to type numbers and the dashes should insert themselves automatically. Example #1: The user types 1 ...
dborovsky's user avatar
  • 131
-1 votes
1 answer
320 views

Better way to format a query string in Java

description I'm using influxDB, I need use Java to generate a query command. The TestingTag struct maybe missing 0-3 fields. code: ...
leaf_yakitori's user avatar
7 votes
2 answers
405 views

Word wrap with file input & ouput

As this is a very basic word wrap program, it has at least the following 3 limitations :- The file should have a terminating newline. The column number at which the lines are to be wrapped must be ...
Kushagr Jaiswal's user avatar
6 votes
1 answer
932 views

Python function to print a two-column table from a dictionary

I'm currently learning Python for a masters and am trying to improve the best I can, as I'll need to know how to code in my future career. I currently have a function that builds a table from a ...
Ramen Enthusiast's user avatar
4 votes
1 answer
215 views

How to make my code shorter. This Code formats Credit card number. Vanilla JS

How to make my script shorter and better? This code allows only digits, auto-format card number like this XXXX XXXX XXXX XXXX. if user hits delete or backspace to delete space in between digits, ...
Max's user avatar
  • 41
1 vote
1 answer
399 views

Read and display JSON results from Google Books API

I’m working with the Google Books API and I’m not so sure about the way I’m going to get the search data and return it to the user. I will describe the path here and I would like to know if you would ...
ARNON's user avatar
  • 401
0 votes
1 answer
50 views

Very unorthodox placement of if-else clauses in C for handling special cases [closed]

I've encountered a situation with an if-else clause. Specifically, the positive case has only 1 expression, whereas the negative case is a sequence of statements wrapped in braces. Like this: ...
DannyNiu's user avatar
  • 310
1 vote
1 answer
107 views

Format multiple dates to look good in text, using functional approach

I had a piece of code that solved really simple problem - it formated two dates to look better on a webpage. It looked like this: ...
Alex l.'s user avatar
  • 111
2 votes
3 answers
254 views

HTML/JavaScript temperature converter with rounding

I am looking to improve the performance of my code through best practices and cleaner code. My goal here is just to perform a conversion whenever there is a keyup. But I noticed that my code looks ...
ARNON's user avatar
  • 401
1 vote
1 answer
146 views

Car price calculator

Im writing this simple program for school and using NumberFormat on all the variables seems very repetitive (Note that we were asked to use the NumberFormat method)....
Grewal_Creator's user avatar

1
2 3 4 5
12